是否需要在 webdriver 的脚本之间添加等待 [英] Is there a need to add waits in between of the script in webdriver

查看:27
本文介绍了是否需要在 webdriver 的脚本之间添加等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 编写我的第一个 webdriver 脚本,该脚本运行良好,但速度非常快.

I am writing my first webdriver script in java, the script is working fine but it's very fast.

例如:- 我的 hmtl 页面中有文本框和下拉元素,我编写了用于发送键和从元素中选择值的脚本,但是当我执行脚本时,速度非常快,是否需要在每次之后进行隐式等待步 ?这值得吗 ?或者还有什么解决方案可以让脚本以流畅的方式运行,并且每一步都正确可见.

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.

下面是我的代码:-

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");   

}

推荐答案

您可以根据需要选择以下任何一种,但不要同时使用隐式和显式等待.导致等待时间不一致,并可能导致 TimeoutExceptions,即使您认为不应该发生.

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.

隐式等待 -

隐式等待是告诉 WebDriver 在尝试查找一个或多个元素(如果它们不是立即可用的)时轮询 DOM 一段时间.

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.

默认设置为 0.一旦我们定义了隐式等待,它将设置为 WebDriver 对象实例的生命周期.

The default setting is 0. Once when we define the implicit wait, it will set for the life of the WebDriver object instance.

它将被写入一次并自动应用于整个会话.一旦我们启动了 Webdriver,它应该立即应用.

It will be written once and applied for entire session automatically. It should be applied immediately once we initiate the Webdriver.

隐式等待不适用于应用程序中的所有命令/语句.它仅适用于FindElement"和FindElements"语句.

Implicit wait will not work all the commands/statements in the application. It will work only for "FindElement" and "FindElements" statements.

如果我们设置了隐式等待,如果元素在第一个实例中没有找到,则 find 元素不会抛出异常,而是会轮询元素直到超时,然后继续进行.

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.

显式等待 -

如果您想根据特定条件进行等待,可以使用显式等待 - 例如visibilityOfWebElement、ElementToBeClickable 等

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")));

显式等待主要用于在执行任何操作后需要等待特定内容/属性更改时,例如当应用程序向系统发出 AJAX 调用并获取动态数据并在 UI 上呈现时.

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.

示例:就像有下拉Country和State一样,根据选择的country值,state下拉列表中的值会发生变化,这需要几秒钟的时间才能根据用户选择获取数据.

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.

流畅的等待 -

使用 FluentWait,我们可以定义等待条件的最长时间,以及检查条件的频率.

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.

并且用户还可以配置为在搜索元素时忽略特定类型的异常,例如NoSuchElementExceptions".findElement(By) 和 findElements(By) 抛出 NoSuchElement 异常.当它试图找到任何元素时,它会返回当前页面上的第一个匹配元素,否则它会抛出 NoSuchElementException - 当没有找到匹配元素时.

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);

这篇关于是否需要在 webdriver 的脚本之间添加等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆