Java 移动仿真器Selenium测试在Jenkins中失败,但在cmd中没有失败

Java 移动仿真器Selenium测试在Jenkins中失败,但在cmd中没有失败,java,jenkins,selenium-webdriver,testng,selenium-chromedriver,Java,Jenkins,Selenium Webdriver,Testng,Selenium Chromedriver,我有一个测试,在詹金斯内部运行时总是失败 我的项目包括SeleniumWebDriver、JAVA、Maven、TestNG、Jenkins和Allure(报告)。 我有一些包含100多个测试用例的测试套件,我通过3种不同的浏览器对它们进行迭代(测试使用TestNG并行运行)。它们都运行(使用maven命令行)并传入我的开发笔记本电脑,使用命令行时在测试服务器上运行 我有两个关于詹金斯的问题,并将它们分为两个问题-其中一个在本问题中描述,另一个(IE11问题)在这里 当在测试服务器的Jenkin

我有一个测试,在詹金斯内部运行时总是失败

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

我有两个关于詹金斯的问题,并将它们分为两个问题-其中一个在本问题中描述,另一个(IE11问题)在这里

当在测试服务器的Jenkins内部运行时,问题就开始了! mobile emulator(Chrome浏览器)中的测试失败-在测试中,我单击一个链接以验证是否使用正确的url打开了一个新窗口。 我尝试了3种类型的点击(Selenium点击、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);
public boolean closePopupWindow(字符串mwh、字符串mTitle、字符串layoutNumber){
//对于IE11-确保在选项中关闭弹出窗口阻止程序。否则它将只有一个窗口句柄并失败
布尔等参元数=false;
字符串newWindow=null;
Set handlers=driver.getWindowHandles();
用于(字符串窗口:处理程序){
如果(!window.equals(mwh)){
新窗口=窗口;
}
}
//焦点在主页上。需要切换到新页面并关闭它
driver.switchTo().window(newWindow);
System.out.println(“现在的焦点是新窗口”);
字符串newTitle=driver.getTitle();
System.out.println(newTitle);
这就是我得到的错误:

java.lang.NullPointerException:entry:handle=null中的null值 位于com.google.common.collect.CollectPremissions.checkEntryNotNull(CollectPremissions.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.carombola.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位

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

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(尝试10秒,间隔500毫秒)构建信息:版本:'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)在移动仿真器中?是的,但它也会传递出去:)