如何在我的自动化测试脚本中替换Thread.sleep()的等待瞬间

时间:2018-01-09 12:03:27

标签: java multithreading selenium selenium-webdriver junit

我是自动化的新蜜蜂,我已经使用 JUnit来开发使用selenium webdriver的自动化测试脚本这里我的要求

我开发了自动化测试脚本。对于我的代码上的一些操作,我想要一些时间来执行下一个命令,比如找到元素并加载页面,所以我使用 Thread.sleep()。所以现在我发现使用Thread.sleep的信息对于制作测试脚本是不利的

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

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 个答案:

答案 0 :(得分:1)

首先,我非常想建议您使用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秒钟。 如果您想知道2之间的区别,请查看链接并查看最适合您的内容

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

答案 1 :(得分:-1)

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

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

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

添加隐式等待后,删除所有Thread.sleep(1000)。在每个语句之后编写Thread.sleep(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类型是布尔返回true或非null返回值,用于所有其他ExpectedCondition类型。

答案 2 :(得分:-2)

由于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. 导航到网址后,您不需要等待。 Selenium将等待浏览器readyStatecomplete

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

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