以下是我为更改密码而编写的测试。
@Test
public void changePassword() throws IOException, InterruptedException {
Login();
driver.findElement(By.xpath(".//*[@id='wrapper']/div[1]/div[2]/div/div/ul/li[3]/a")).click();
driver.findElement(By.xpath(".//*[@id='wrapper']/div[1]/div[2]/div/div/ul/li[3]/ul/li[3]/a")).click();
driver.findElement(By.xpath(".//*[@id='change-password']/div[1]/div/input")).sendKeys("abcd");
driver.findElement(By.xpath(".//*[@id='Users_password']")).sendKeys("xyz123456");
driver.findElement(By.xpath(".//*[@id='repassword']")).sendKeys("xyz123456");
driver.findElement(By.xpath(".//*[@id='change-password']/div[4]/div/input")).click();
}
}
下面是我得到的错误,我已经检查过Xpath是否正确,仍然收到此错误 -
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='change-password']/div[1]/div/input"}
Command duration or timeout: 2.81 seconds
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
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:215)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:509)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
at pages.ChangePass.changePassword(ChangePass.java:16)
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:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='change-password']/div[1]/div/input"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'DESKTOP-1AKPPH7', ip: '192.168.56.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/Avinash/AppData/Local/Temp/anonymous9034314523683234934webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:11390)
at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/Avinash/AppData/Local/Temp/anonymous9034314523683234934webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:11399)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/k(file:///C:/Users/Avinash/AppData/Local/Temp/anonymous9034314523683234934webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12879)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/Avinash/AppData/Local/Temp/anonymous9034314523683234934webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12884)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/Avinash/AppData/Local/Temp/anonymous9034314523683234934webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12826)
我的问题是 - 1.上面的代码有什么问题,当我执行它时,它会转到更改密码屏幕。 2.以上是测试更改密码功能的最佳方法吗? 3.是否还有其他测试方案,您可以推荐我加入以及如何包含?
答案 0 :(得分:2)
首先尝试添加HTML代码段,以便可以以更加冗余的方式编写定位器。
第二,使用一些等待机制正确加载页面。在您的情况下您正在单击链接并尝试在更改密码输入字段中输入数据,由于页面未正确加载而Selenium尝试在更改密码字段中输入值而获得的例外。
对于一个简单的解决方案,您可以使用隐式等待,它将起作用
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
答案 1 :(得分:1)
以下是您问题的答案:
cell.button.tag = indexpath.item
:您构建的What is wrong in above code
,例如xpaths
,.//*[@id='wrapper']/div[1]/div[2]/div/div/ul/li[3]/a
和.//*[@id='wrapper']/div[1]/div[2]/div/div/ul/li[3]/ul/li[3]/a
是绝对.//*[@id='change-password']/div[1]/div/input
,其中包含xpaths
。因此,他们 index
和 brittle
。您可以考虑构建唯一的 vulnerable
。
logical xpath
:我认为这方面没有最好的做法。因为它真的取决于你想要解决的问题。但是,在对文本字段使用Is above way the best way to test change password functionality?
方法之前,您应该首先考虑使用sendKeys("abcd")
方法。
clear()
,我们应该考虑使用NoSuchElementException: Unable to locate element
来ExplicitWait
,因此 ExpectedConditions
可以找到该元素至少。如果这回答你的问题,请告诉我。