SeleniumWebDriver(Java):如何嵌套这些NoTouchElement异常测试?
操作系统:Windows7 32位 ChromeDriver版本:2.30 Selenium Webdriver版本:3.4.0 爪哇8 我尝试了几种不同的方法来清理代码,而不必重复相同的try/catch块。我试图检查我正在测试的页面上是否存在各种元素。我可以优雅地向控制台报告,这段代码确实可以正常工作。 我现在遇到的问题是代码不太好。有没有办法嵌套这些try/catch块,或者将它们放入if/else循环中SeleniumWebDriver(Java):如何嵌套这些NoTouchElement异常测试?,java,selenium-webdriver,Java,Selenium Webdriver,操作系统:Windows7 32位 ChromeDriver版本:2.30 Selenium Webdriver版本:3.4.0 爪哇8 我尝试了几种不同的方法来清理代码,而不必重复相同的try/catch块。我试图检查我正在测试的页面上是否存在各种元素。我可以优雅地向控制台报告,这段代码确实可以正常工作。 我现在遇到的问题是代码不太好。有没有办法嵌套这些try/catch块,或者将它们放入if/else循环中 try { driver.findElement(By.xp
try {
driver.findElement(By.xpath("/html/head/title"));
System.out.println("Title found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Title NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.xpath("//*[@id='logindiv']"));
System.out.println("Login form found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Login form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("username"));
System.out.println("'Username' field found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("'Username' form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("password"));
System.out.println("'Password' field found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("'Password' form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("loginSubmit")).getText();
System.out.println("Login button found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Login button NOT FOUND...");
// ex.printStackTrace();
}
有几件事
.findElement()
替换.findElement()
并测试是否为空。参见#2中的示例Assert
库,如JUnit等。它使这些验证变得更加容易/干净
整件事
try
{
driver.findElement(By.id("username"));
System.out.println("'Username' field found...");
Thread.sleep(1000);
}
catch (NoSuchElementException ex)
{
System.out.println("'Username' form NOT FOUND...");
// ex.printStackTrace();
}
应该/可能看起来像
Assert.assertFalse(driver.findElements(By.id("username")).isEmpty(), "Validate Username field exists");
by.XPath(“/*[@ID='logindiv']”)
。这比.id(“logindiv”)更简单、更容易阅读Thread.Sleep()
是一种不好的做法,应该避免。改用WebDriverWait
。探索ExpectedConditions
,查看所有可以等待的内容,并充分利用它们。在您在代码中发布的案例中,我看不出有任何理由等待这些,因此这是几秒钟不必要的浪费时间.getText()
,但不使用它。由于您只是检查按钮是否存在,因此可以安全地从末尾删除.getText()
.findElement()
替换.findElement()
并测试是否为空。参见#2中的示例Assert
库,如JUnit等。它使这些验证变得更加容易/干净
整件事
try
{
driver.findElement(By.id("username"));
System.out.println("'Username' field found...");
Thread.sleep(1000);
}
catch (NoSuchElementException ex)
{
System.out.println("'Username' form NOT FOUND...");
// ex.printStackTrace();
}
应该/可能看起来像
Assert.assertFalse(driver.findElements(By.id("username")).isEmpty(), "Validate Username field exists");
by.XPath(“/*[@ID='logindiv']”)
。这比.id(“logindiv”)更简单、更容易阅读Thread.Sleep()
是一种不好的做法,应该避免。改用WebDriverWait
。探索ExpectedConditions
,查看所有可以等待的内容,并充分利用它们。在您在代码中发布的案例中,我看不出有任何理由等待这些,因此这是几秒钟不必要的浪费时间.getText()
,但不使用它。由于您只是检查按钮是否存在,因此可以安全地从末尾删除.getText()
public void checkElementVisibile()throws InterruptedException {
element = driver.findElement(By.xpath("/html/head/title"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("Title found...");
else
System.out.println("Title not found...");
element = driver.findElement(By.xpath("//*[@id='logindiv']"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("Login form found...");
else
System.out.println("Login form not found...");
element = driver.findElement(By.id("username"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("'Username' field found...");
else
System.out.println("'Username' field not found...");
element = driver.findElement(By.id("password"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("'Password' field found...");
else
System.out.println("'Password' field not found...");
}
public static boolean isElementVisibile(WebElement element){
try {
return element.isDisplayed();
}catch (NoSuchElementException ex)
{
return false;
}
}
您可以尝试以下方法:
public void checkElementVisibile()throws InterruptedException {
element = driver.findElement(By.xpath("/html/head/title"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("Title found...");
else
System.out.println("Title not found...");
element = driver.findElement(By.xpath("//*[@id='logindiv']"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("Login form found...");
else
System.out.println("Login form not found...");
element = driver.findElement(By.id("username"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("'Username' field found...");
else
System.out.println("'Username' field not found...");
element = driver.findElement(By.id("password"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("'Password' field found...");
else
System.out.println("'Password' field not found...");
}
public static boolean isElementVisibile(WebElement element){
try {
return element.isDisplayed();
}catch (NoSuchElementException ex)
{
return false;
}
}