更改密码测试用例 Selenium - 调试
Change Password Test Cases Selenium - Debug
下面是我写的更改密码的测试。
@测试
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. 是否还有更多测试场景可以推荐我包含以及如何包含?
以下是您问题的答案:
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
是包含 index
的绝对 xpaths
。因此它们是 brittle
和 vulnerable
。可以考虑构造unique logical xpath
.
Is above way the best way to test change password functionality?
:
我认为这方面没有最佳实践。因为这实际上取决于您要解决的问题。但绝对在对文本字段使用 sendKeys("abcd")
方法之前,您应该首先考虑使用 clear()
方法。
- 当有
NoSuchElementException: Unable to locate element
时,我们应该考虑用适当的ExpectedConditions
诱导ExplicitWait
,这样Selenium
就可以定位到元素至少。
如果这回答了您的问题,请告诉我。
首先尝试添加 HTML 代码片段,这样定位器就可以用更冗余的方式编写。
2、使用一些等待机制来正确加载页面。在您的情况下,您正在单击 link 并尝试在更改密码输入字段中输入数据,由于页面未正确加载和 Selenium 尝试在更改密码字段中输入值而导致出现异常.
对于简单的解决方案,您可以使用隐式等待,它会起作用
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
下面是我写的更改密码的测试。
@测试
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. 是否还有更多测试场景可以推荐我包含以及如何包含?
以下是您问题的答案:
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
是包含 index
的绝对 xpaths
。因此它们是 brittle
和 vulnerable
。可以考虑构造unique logical xpath
.
Is above way the best way to test change password functionality?
:
我认为这方面没有最佳实践。因为这实际上取决于您要解决的问题。但绝对在对文本字段使用 sendKeys("abcd")
方法之前,您应该首先考虑使用 clear()
方法。
- 当有
NoSuchElementException: Unable to locate element
时,我们应该考虑用适当的ExpectedConditions
诱导ExplicitWait
,这样Selenium
就可以定位到元素至少。
如果这回答了您的问题,请告诉我。
首先尝试添加 HTML 代码片段,这样定位器就可以用更冗余的方式编写。
2、使用一些等待机制来正确加载页面。在您的情况下,您正在单击 link 并尝试在更改密码输入字段中输入数据,由于页面未正确加载和 Selenium 尝试在更改密码字段中输入值而导致出现异常.
对于简单的解决方案,您可以使用隐式等待,它会起作用
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);