SeleniumWebDriverJava-POM-console结果指向测试用例中未使用的元素
当测试用例失败并且控制台输出说找不到元素时,遇到了一个问题,但是,这个元素甚至没有在测试用例中使用。 我正在使用页面对象模型模式。 它是否与类元素的执行顺序有关 测试用例:SeleniumWebDriverJava-POM-console结果指向测试用例中未使用的元素,java,selenium,xpath,pom.xml,Java,Selenium,Xpath,Pom.xml,当测试用例失败并且控制台输出说找不到元素时,遇到了一个问题,但是,这个元素甚至没有在测试用例中使用。 我正在使用页面对象模型模式。 它是否与类元素的执行顺序有关 测试用例: public class FilterResultTestCaseActiveStudentsOnly { public static void main(String[] args) throws InterruptedException { System.out.println(" enter username
public class FilterResultTestCaseActiveStudentsOnly {
public static void main(String[] args) throws InterruptedException {
System.out.println(" enter username ");
Login.userName.sendKeys(Account.loginElenaB);
Thread.sleep(3000);
System.out.println(" enter password ");
Login.password.sendKeys(Account.pswdElenaB);
Thread.sleep(3000);
System.out.println(" click login btn ");
Login.loginButton.click();
Thread.sleep(3000);
System.out.println("click on Students page ");
NavBar.studentsPage.click();
Thread.sleep(6000);
System.out.println("click on Filter Results Dropdown ");
FiltersResultDropdown.clickOnFilterResultsDropdown.click();
Thread.sleep(3000);
System.out.println("select Active Students only radiobutton ");
FiltersResultDropdown.activeStudentsOnlyFilterResultsDropdown.click();
Thread.sleep(3000);
System.out.println("click on Apply Filters Results Dropdown ");
FiltersResultDropdown.applyFilterButtonFilterResultsDropdown.click();
Thread.sleep(3000);
StudentsPage.studentLinkClickStudentsPage.click();
Thread.sleep(3000);
String activeStudentsOnlyText = GetTextForAsserts.getStatusActiveText.getText();
try{
Assert.assertEquals("STATUS: Active", activeStudentsOnlyText);
System.out.println("The student is active");
} catch(AssertionError e){ //What if there are not students filtered? ToDo!
System.out.println("The student not found");
;
throw e;
}
对于断言,我创建了一个类,在其中我找到了必要的元素:
班级:
public class GetTextForAsserts {
WebDriverSettings driver;
public static WebElement getLetterOneText = WebDriverSettings.driver.findElement(By.xpath("//*[@id=\"studentLetters\"]/tr[5]/td[4]"));
public static WebElement getChronicLetterText = WebDriverSettings.driver.findElement(By.xpath("//*[@id=\"studentLetters\"]/tr[4]/td[4]"));
public static WebElement getStatusActiveText = WebDriverSettings.driver.findElement(By.xpath("//*[@id=\"studentOverview\"]/div[1]/div/div[4]/div[1]")); etc.
}
因此,它所做的是——测试用例失败,控制台说无法定位getLetterOneText(或其他元素),但它甚至没有在测试用例中使用。
所有元素的XPath都是正确的(即使对于“未定位”的元素也是如此),经过多次仔细检查。
为了不让测试用例失败,我必须注释掉它所指向的“未定位”元素,但它不是解决方案。您的
GetTextForAsserts
类将为所有三个元素(包括getLetterOneText
)清除页面。因此,当您实例化该类时,无论是否使用该类,它都会刮取页面
更好的做法是让变量通过类存储定位器,而不是存储元素。这样,您就可以调用一个方法,在需要时使用定位器来刮取页面
您应该花一些时间查看页面对象模型教程和参考资料,以了解执行此类操作的更多标准方法。此外,您还应该花一些时间学习如何调试自己的程序。如果您仔细阅读代码,您会看到这些行的执行,并知道问题所在