【问题标题】:Change Password Test Cases Selenium - Debug更改密码测试用例 Selenium - 调试
【发布时间】:2017-12-08 13:42:52
【问题描述】:

以下是我为更改密码编写的测试。

@测试

    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. 有没有更多的测试场景可以推荐我包括在内?

【问题讨论】:

  • 为什么这个问题被否决了?请说明原因,以便OP可以改进问题。谢谢
  • 肯定会有所帮助,然后我可以纠正它,谢谢 Rahal

标签: java xpath selenium-webdriver testng forgot-password


【解决方案1】:

以下是您问题的答案:

  1. 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。因此它们是 brittlevulnerable。你可以考虑构造唯一的logical xpath

  1. Is above way the best way to test change password functionality?:

我认为这方面没有最佳做法。因为这实际上取决于您要解决的问题。但绝对在对文本字段使用sendKeys("abcd") 方法之前,您应该首先考虑使用clear() 方法。

  1. 当有NoSuchElementException: Unable to locate element时,我们应该考虑用正确的ExpectedConditions来诱导ExplicitWait,这样Selenium可以至少定位到元素。

如果这能回答您的问题,请告诉我。

【讨论】:

  • 非常感谢,我使用了显式等待,问题解决了。我使用由 firepath 生成的 xpaths,你认为我应该创建自己的 xpaths 吗?您对使用的最佳定位器有什么建议以及要遵循的最佳实践。此外,什么是绝对与逻辑 xpath。我知道我已经问了很多,但如果从一个有很多经验的人那里知道会很好。此外,我正在尝试测试更改密码字段的功能,因此在您建议的许多测试用例中应该是自动化的理想选择。我们正在从旧案例转向自动化
  • @AvinashPandey 很高兴能为您提供帮助 :) 将firepath 生成的absolute xpath 视为创建您自己独特的logical xpath 的参考。按时间顺序排列的最佳定位器:idnamelinkTextcssxpath。您能否考虑将 Accept Answer 作为您的 的解决方案Question 点击我的答案旁边的勾号,就在向上/向下投票箭头下方,所以勾号变成绿色?谢谢
【解决方案2】:

首先尝试添加 HTML sn-p ,这样定位器可以以更冗余的方式编写。

第二,使用一些等待机制来正确加载页面。在您的情况下,您正在单击一个链接并尝试在更改密码输入字段中输入数据,由于页面未正确加载以及 Selenium 尝试在更改密码字段中输入值而导致的异常。

对于一个简单的解决方案,您可以使用隐式等待,它会起作用

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

【讨论】:

  • 它解决了这个问题,非常感谢。此外,我在全球范围内使用它,认为它会起作用,但似乎没有。此外,您能否提出更多方案并验证我使用的是否是最好的方法,或者是否有更好的实现。请建议。再次感谢所有帮助。非常感谢。
  • 这也是一个解决方案。但不是最好的。因为您对等待时间进行了硬编码。考虑一下服务器加载时间超过 5 秒的场景。
  • @Rahal Kanishka 这是隐式等待,它将等待最多 5 秒,在一般产品线场景中,我们将其设置为 30 秒或 60 秒,它将等待最长到提供的时间.为了考虑他的场景,我让他使用 5 秒。此外,它不像 Thread.sleep() 那样硬编码。
  • @Arun 据我所知 Thread.sleep() 和隐式等待不推荐太多,推荐的方法是使用显式等待。 WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
  • 是的,没错,但对于初学者来说,隐式等待是可取的。
猜你喜欢
  • 2016-06-22
  • 1970-01-01
  • 2011-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
相关资源
最近更新 更多