在我的一个测试用例中,我试图找到登录后是否显示一个元素。
softAssert.assertTrue(landing1PageFuncs.getSetupBtn().isDisplayed(), "Login Failed");
softAssert.assertAll();
getSetupBtn getter方法如下
public WebElement getSetupBtn() {
return AppWait.waitForElement(driver, setupBtn , Constants.WAITTIME);
}
waitForElement函数如下
public static WebElement waitForElement(WebDriver driver, WebElement element, int timeOutInSeconds) {
try {
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
element = wait.until(ExpectedConditions.visibilityOf(element));
driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_ELEMENT, TimeUnit.SECONDS); //reset implicitlyWait
} catch (Exception e) {
logger.warn(element + " not found even after waiting for: " + timeOutInSeconds + " seconds");
logger.warn(e, new RuntimeException());
}
return element;
}
但我观察到的是isDisplayed方法永远不会被调用。因此,软判断不会被解雇。
我得到的只是例外如下:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='page_content_inner']/div/a[1]/div"}
Command duration or timeout: 19 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'justdial', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-86-generic', java.version: '1.8.0_91'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0.1, platform=LINUX, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 6b9e0607-ee41-42b8-bb58-0f7d6cc32481
*** Element info: {Using=xpath, value=//*[@id='page_content_inner']/div/a[1]/div}
at sun.reflect.GeneratedConstructorAccessor13.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:500)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.openqa.selenium.support.events.EventFiringWebDriver$2.invoke(EventFiringWebDriver.java:103)
at com.sun.proxy.$Proxy9.findElement(Unknown Source)
at org.openqa.selenium.support.events.EventFiringWebDriver.findElement(EventFiringWebDriver.java:188)
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy11.isDisplayed(Unknown Source)
at org.justdial.jdomni.functionaltests.LoginPageTests.chkLoginWithValidCred(LoginPageTests.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
我做错了什么?我正在寻找直到断言并说“登录失败”的解决方案
任何帮助表示感谢。
答案 0 :(得分:1)
无法找到您的元素,因此无法确定isDisplayed()
。 isDisplayed()
只能在存在且已经找到的元素上调用,这就是为什么永远不会调用断言的原因。
landing1PageFuncs.getSetupBtn()
在调用isDisplayed()
之前抛出异常。
如果您想知道网页上是否存在元素,则应使用driver.findElements(By.xpath('locator')).size > 0
代替driver.findElement(By.xpath('locator').isDisplayed()
答案 1 :(得分:0)
Since the element is not available in DOM, u cannot apply a soft assertion and obviously u'll get NoSuchElementException.
you can write a utility method like below to verify the whether the locator exists or not in the DOM.
public class verifyloc {
public static void main(String a[]) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
String loc = "//input[@id='lst-ib']";
By locator = verifyElementPresent(driver, loc);
System.out.println("Locator is : " + locator);
}
public static By verifyElementPresent(WebDriver driver, String locator) {
try {
driver.findElement(By.xpath(locator));
return By.xpath(locator);
} catch (Throwable e) {
return null;
}
}
}