代码之家  ›  专栏  ›  技术社区  ›  MohanRaj

如何替换线程的等待时间。我的自动化测试脚本中的sleep()

  •  -1
  • MohanRaj  · 技术社区  · 7 年前

    我是自动化领域的新手,我使用了 JUnit使用selenium webdriver开发自动化测试脚本 在这里我的要求

    我已经开发了自动化测试脚本。对于代码中的某些操作,我需要一些时间来执行下一个命令,如查找元素并加载页面,以便使用 线睡眠() . 现在我找到了关于使用线程的信息。睡眠不利于编写测试脚本

    所以我的问题是 有人能告诉我如何给我的代码提供动态等待时间吗 我的代码如下,

    System.setProperty("webdriver.chrome.driver", "//path of the chrome driver");
    driver = new ChromeDriver();
    
    driver.get("https://url for my org");
    Thread.sleep(5000); //to load the login page of my Org
    driver.findElement(By.xpath("my login xpath")).click();
    Thread.sleep(1000);
    driver.findElement(By.xpath("my login xpath")).sendKeys("username");
    Thread.sleep(2000);
    driver.findElement(By.xpath("my password xpath")).click();
    Thread.sleep(1000);
    driver.findElement(By.xpath("my password xpath")).sendKeys("my password");
    driver.findElement(By.xpath("login button")).click();
    Thread.sleep(6000); //for giving the time to login Org
    

    根据我的要求,我在代码上使用了不同类型的等待时间。 如果我去等待课程,我能在 那里

    我的问题不同,因为获得明确的等待是为不同的地方提供恒定的等待时间,如我们定义如下,

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element_xpath")));
    element.click();
    //for another wait 
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element_xpath")));
    element.sendKeys("something"); //Here it's wait time(10 seconds) is same to the after the click action wait time. 
    

    那么,为什么我在没有需要的情况下,同时等待我所有的等待部分呢?如果有其他方法请告诉我?

    如果我错了,请纠正我。

    谢谢

    3 回复  |  直到 7 年前
        1
  •  3
  •   Anand    7 年前

    首先,我很想建议你使用pageobjects或类似的东西。这会让你陷入维修地狱。话虽如此,为了解决问题,您可以使用隐式或显式等待,以最适合您的为准。

    如果要使用隐式等待,请在初始化驱动程序时使用:

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    

    如果要使用显式等待,请使用此示例并将其设置为您自己的:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    WebElement element = 
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
    
    element.click();
    

    在这两个示例中,驾驶员最多会等待10秒。 如果你想知道这两种方法的区别,请查看链接,看看什么最适合你

    http://toolsqa.com/selenium-webdriver/implicit-explicit-n-fluent-wait/

        2
  •  -1
  •   JeffC    7 年前

    根据Selenium贡献者的建议,应该避免使用隐式等待。您还应该避免混合使用所述的隐式和显式等待 in the Selenium docs :

    警告:不要混合隐式和显式等待。这样做可能会导致无法预测的等待时间。

    相反,您应该使用显式等待,例如。 WebDriverWait . 在将数据输入字段之间不需要等待。我的一般方法是等待特定元素在页面上可见,以指示页面已完成加载。如果您有一个很长的加载/动态部分,您需要选择最后一个要加载到页面上的元素之一。加载该元素后,您可以在页面上随意输入文本等。唯一需要在同一页面上再次等待的时间是触发某些动态行为,例如单击刷新部分页面的链接,从下拉列表中进行选择,触发重新加载另一个下拉列表,等等。

    您的代码应该更像

    driver.get("https://url for my org");
    driver.findElement(By.xpath("my login xpath")).sendKeys("username");
    driver.findElement(By.xpath("my password xpath")).sendKeys("my password");
    driver.findElement(By.xpath("login button")).click();
    // wait for element on newly loaded page and continue
    
    1. 导航到URL后不需要等待。Selenium将等待浏览器 readyState 成为 complete .

    2. 在使用之前,无需单击元素 .sendKeys() .

    3. 通常,在将数据输入字段之间不需要等待。

        3
  •  -1
  •   Shoaib Akhtar    7 年前

    您应该在程序中添加隐式等待,如下所示

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("https://url for my org");
    

    这里隐式等待基本上是告诉WebDriver,如果指定元素在UI(DOM)上不可用,它应该等待10秒(如代码中所述),即在引发异常之前,它将等待10秒。

    添加隐式等待后,请删除所有线程。睡眠(1000)。写线程不是一种好的做法。每次陈述后睡眠(1000)。

    或者可以使用显式等待,如

    // Create object of WebDriverWait class
    WebDriverWait wait=new WebDriverWait(driver,20);    
    // Wait till the element is not visible    
    WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("ur xpath here")));
    boolean status = element.isDisplayed();
    //Then perform other action on element
    

    这将在抛出TimeoutException之前等待20秒,或者如果找到元素,将在0-20秒后返回它。默认情况下,WebDriverWait每隔500毫秒调用一次ExpectedCondition,直到它成功返回。对于所有其他ExpectedCondition类型,成功返回is for ExpectedCondition type is Boolean return true或not null返回值。