我创建了一个 Maven 项目,其中包含使用 Selenium Webdriver (java) 进行的 20 个测试。现在,当我想执行 Maven 项目时,有时会出现以下错误:
这是由于每次测试都要登录造成的。因此,当我想运行 20 个测试时,有时会出现该错误,并且我无法继续测试,因此它在 Selenium Webdriver 中返回“失败的测试”。
有人知道如何解决这个问题吗? 我尝试将“Thread.sleep(30000);”在每次测试结束时给他们一些时间“不要显得像机器人”,但这不起作用......
非常感谢大家的帮助!
最佳答案
这是您问题的答案:
真正的问题:
- 您正在使用的 URL/连接(如果是
Not Secure
)那么每当您通过 Mozilla Firefox 53.0 访问该 URL 时,Firefox 都会在地址栏中显示带有红色删除线的锁定图标。现在,当加载 URL 时,默认情况下光标将位于Username
上。字段,将会弹出显示一条消息This connection is not secure. Logins entered here could be compromised. Learn More
像这样:
- 现在,您的脚本通过 Selenium 在
Username
中输入用户名输入字段和Not Secure
弹出窗口覆盖Password
输入字段。 - 接下来,如果您尝试调用
click()
或sendKeys()
操作于Password
输入字段Not Secure
弹出窗口收到点击并Insecure password warning in Firefox
页面将在下一个选项卡中打开,Selenium 将其焦点转移到新选项卡。因此测试用例开始失败。
解决方案:
在这些情况下,最好的解决方案是:
- 创建新的 Mozilla Firefox 配置文件。您会发现documentation here 。例如,我创建了一个名为
debanjan
的 Firefox 配置文件 - 配置 Firefox 配置文件
debanjan
忽略所有UntrustedCertificate
问题。 - 重新运行您的测试脚本,没有任何问题。
以下是禁用
insecure_field_warning
的示例代码块:System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); ProfilesIni profile = new ProfilesIni(); FirefoxProfile testprofile = profile.getProfile("debanjan"); testprofile.setAcceptUntrustedCertificates(true); testprofile.setAssumeUntrustedCertificateIssuer(true); testprofile.setPreference("security.insecure_field_warning.contextual.enabled", false); DesiredCapabilities dc = DesiredCapabilities.firefox(); dc.setCapability(FirefoxDriver.PROFILE, testprofile); dc.setCapability("marionette", true); WebDriver driver = new FirefoxDriver(dc); driver.manage().window().maximize(); driver.navigate().to("http://demosite.center/wordpress/wp-login.php");
如果这能回答您的问题,请告诉我。
关于java - Firefox selenium webdriver 给出 "Insecure Connection",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44716870/