【问题标题】:Mobile emulator Selenium tests fail in Jenkins but not in cmd移动模拟器 Selenium 测试在 Jenkins 中失败,但在 cmd 中失败
【发布时间】:2018-07-31 23:00:49
【问题描述】:

我有一个在 Jenkins 中运行时总是失败的测试。

我的项目包括 Selenium webdriver、JAVA、Maven、TestNG、Jenkins、Allure(报告)。 我有一些包含 100 多个测试用例的测试套件,我通过 3 个不同的浏览器对它们进行迭代(测试使用 TestNG 并行运行)。它们都运行(使用 maven 命令行)并传入我的开发笔记本电脑,并在使用命令行时在测试服务器上传递。

我有 2 个关于 Jenkins 的问题并将它们分成 2 个问题 - 其中一个在这个问题中描述,另一个(IE11 问题)在这里。

在测试服务器的 Jenkins 中运行时,问题就开始了! 移动模拟器(Chrome 浏览器)中的测试失败 - 在测试中,我单击一个链接以验证是否使用正确的 url 打开了一个新窗口。 我尝试了 3 种类型的点击(Selenium click、Actions、JS)并且都返回了一个空句柄。

代码:

这里我创建主窗口句柄并点击链接:

String mwh = driver.getWindowHandle();
WebElement poweredBy = (new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(Consts.POWERED_BY_XPATH_1000))));
poweredBy.click();

这是获取句柄并验证新窗口的方法的一部分:

public boolean closePopupWindow(String mwh, String mTitle, String layoutNumber) {
// For IE11- make sure popup blocker is turned off in the options. else it will have only one window handle and fail
boolean isOpenedWindowCorrect = false; 
String newWindow = null;
Set<String> handlers = driver.getWindowHandles();

for (String window : handlers) {
  if (!window.equals(mwh)) {
    newWindow = window;
  }
}
// the focus is on the main page. need to switchTo new page and close it
driver.switchTo().window(newWindow);
System.out.println("The focus now is on the NEW window");
String newTitle = driver.getTitle();
System.out.println(newTitle);

这是我得到的错误:

java.lang.NullPointerException:条目中的 null 值:handle=null 在 com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:34) 在 com.google.common.collect.SingletonImmutableBiMap.(SingletonImmutableBiMap.java:42) 在 com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:73) 在 com.google.common.collect.ImmutableMap.of(ImmutableMap.java:123) 在 org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.window(RemoteWebDriver.java:995) 在 il.carambola.pages.Page.closePopupWindow(Page.java:786)

您认为 Jenkins 不会在浏览器中打开新窗口存在安全问题吗?打开窗户很慢吗? 不使用移动模拟器时,相同的测试通过。 (我在 Chrome 和 Firefox 中进行过同样的测试,点击成功,通过验证)。

JDK 1.8.0_162

詹金斯 V 2.121.1

服务器 - AWS t2.large - 8GB RAM,Windows server 2016 数据中心,64 位

【问题讨论】:

    标签: java jenkins selenium-webdriver testng selenium-chromedriver


    【解决方案1】:

    很明显,当你的程序到达这一行时

    driver.switchTo().window(newWindow);
    

    newWindow 仍然为空。这是编写方式,可能是时间问题,也可能是导致弹出窗口不显示的另一个问题。为了确保这不是时间问题,我建议在尝试切换窗口之前添加某种等待,等待有多个窗口句柄。类似的东西

    (new WebDriverWait(driver,10)).until(d -> d.getWindowHandles().size() == 2);
    

    如果等待失败,那么您知道弹出窗口被阻止并且可以从那里继续。

    【讨论】:

    • 谢谢 Micah,我会在几秒钟内尝试一下(即使我在进入 closePopupWindow() 之前确实给了它 Thread.sleep(4000)。它不在我上面的示例中)。我知道箭头“->”是一个 Lambda 表达式。它会在 IntelliJ 之外工作吗?
    • 嘿,它又没用 :( 这是我得到的错误:预期条件失败:等待 il.carambola.pages.Page$$Lambda$245/942575179@ 7d32efcd(以 500 MILLISECONDS 间隔尝试 10 秒)构建信息:版本:'3.5.2',修订:'10229a9',时间:'2017-08-21T17:29:55.15Z' 系统信息:主机: 'EC2xxxx', ip: 'xx.xx.xx.x', os.name: 'Windows Server 2016', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_161 ' 驱动信息:org.openqa.selenium.chrome.ChromeDriver....
    • 这是否在移动模拟器中本地传递(不在 Jenkins 中)?
    • 是的,但它也会通过它:)
    猜你喜欢
    • 2016-10-07
    • 2013-05-23
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 2020-01-01
    • 2011-09-16
    • 2021-06-02
    • 1970-01-01
    相关资源
    最近更新 更多