问题
I am writing my first webdriver script in java, the script is working fine but it's very fast.
For eg :- I have textboxes and dropdown elements in my hmtl page and i wrote the script for sending keys and selecting values from the elements but when I execute the script it's very fast is there any need to give implicit waits after every step ? Is it worth ? Or what else is the solution so that the script runs in a smooth way that every step is visible properly.
Below is my code :-
public static void main(String[] args) {
WebDriver wb = new FirefoxDriver();
wb.manage().window().maximize();
wb.navigate().to("http://newtours.demoaut.com/");
/*WebDriverWait wait1 = new WebDriverWait(wb, 50);
WebElement element1 = wait1.until(ExpectedConditions.
elementToBeClickable(By.xpath("//img[@alt='Mercury Tours']")));*/
wb.findElement(By.xpath("//a[text()='REGISTER']")).click();
wb.findElement(By.xpath("//input[@name='firstName']")).sendKeys("Rameshwari");
wb.findElement(By.xpath("//input[@name='lastName']")).sendKeys("Nayak");
wb.findElement(By.xpath("//input[@name='phone']")).sendKeys("7208471118");
wb.findElement(By.xpath("//input[@id='userName']")).sendKeys("Rama");
wb.findElement(By.xpath("//input[@name='address1']")).sendKeys("Nithyanand Chawl");
wb.findElement(By.xpath("//input[@name='city']")).sendKeys("Mumbai");
wb.findElement(By.xpath("//input[@name='state']")).sendKeys("Maharashtra");
wb.findElement(By.xpath("//input[@name='postalCode']")).sendKeys("4000017");
Select dd = new Select(wb.findElement(By.xpath("//select[@name = 'country']")));
dd.selectByVisibleText("INDIA");
}
回答1:
You can choose any of below based on your need, but do not use implicit and explicit waits at the same time. This will result in inconsistent wait times, and can result in TimeoutExceptions even when you think there should not be any.
Implicit Wait -
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.
The default setting is 0. Once when we define the implicit wait, it will set for the life of the WebDriver object instance.
It will be written once and applied for entire session automatically. It should be applied immediately once we initiate the Webdriver.
Implicit wait will not work all the commands/statements in the application. It will work only for "FindElement" and "FindElements" statements.
If we set implicit wait, find element will not throw an exception if the element is not found in first instance, instead it will poll for the element until the timeout and then proceeds further.
Explicit Wait -
You can use explicit wait if you want to wait based on certain condition - like visibilityOfWebElement, ElementToBeClickable etc.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("xyz")));
Explicit wait is mostly used when we need to Wait for a specific content/attribute change after performing any action, like when application gives AJAX call to system and get dynamic data and render on UI.
Example: Like there are drop-downs Country and State, based on the country value selected, the values in the state drop-down will change, which will take few seconds of time to get the data based on user selection.
Fluent Wait -
Using FluentWait we can define the maximum amount of time to wait for a condition, as well as the frequency with which to check for the condition.
And also the user can configure to ignore specific types of exceptions such as "NoSuchElementExceptions" when searching for an element. NoSuchElement exception is thrown by findElement(By) and findElements(By). When ever it try to find any element it returns the first matching element on the current page else it throws NoSuchElementException - when no matching elements are found.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
//Wait for the condition
.withTimeout(30, TimeUnit.SECONDS)
// which to check for the condition with interval of 5 seconds.
.pollingEvery(5, TimeUnit.SECONDS)
//Which will ignore the NoSuchElementException
.ignoring(NoSuchElementException.class);
回答2:
Giving implicit wait property once in your code is enough, it is applicable to all the statements in your code. you need to put wait only in case where you are not able to find an element because of page load. Faster the better from performance point if the script is working as expected.
来源:https://stackoverflow.com/questions/37939413/is-there-a-need-to-add-waits-in-between-of-the-script-in-webdriver