我正在尝试使用Selenium和IE 11模拟登录场景。每当Selenium启动IE时,它都会使用随机端口打开一个页面,然后导航到给定的URL。但是Selenium尝试在刷新新页面或切换到新页面之前找到元素。我正在使用下面的初始化和测试代码。
驱动器初始化
public void init(){
caps = DesiredCapabilities.internetExplorer();
InternetExplorerOptions ieOpts = new InternetExplorerOptions();
caps.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);
caps.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, false);
caps.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);
caps.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
caps.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
caps.setCapability(InternetExplorerDriver.LOG_LEVEL, "ERROR");
ieOpts.requireWindowFocus();
ieOpts.merge(caps);
if ( environment.equalsIgnoreCase("local") ) {
System.setProperty("webdriver.ie.driver", Configuration.getProperty("ie.driver.windows.path"));
webDriver.set(new InternetExplorerDriver(ieOpts.merge(caps)));
}
}
测试用例
@Before
public void setUp() throws Exception {
Driver.getInstance().setDriver("internet explorer", "Windows 7", "local");
driver = Driver.getInstance().getDriver();
baseUrl = "http://xxx.yyy.com";
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS).pageLoadTimeout(100, TimeUnit.SECONDS);
driver.manage().window().maximize();
//driver.get(baseUrl);
}
@Test
public void testLoginPolicySearch() throws Exception {
driver.get("http://xxxx.yyyy.com/en/Login.jsp");
driver.findElement(By.id("userid")).clear();
driver.findElement(By.id("userid")).sendKeys("dpoddar");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("weqweqwe");
driver.findElement(By.id("password")).sendKeys(Keys.ENTER);
}
例外
org.openqa.selenium.NoSuchWindowException: Currently focused window has been closed.
Build info: version: '3.8.0', revision: '924c4067df', time: '2017-11-30T11:20:39.408Z'
System info: host: 'sssssss', ip: 'ssssss', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: false, ie.browserCommandLineSwitches: , ie.ensureCleanSession: true, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: true, ignoreZoomSetting: true, initialBrowserUrl: http://localhost:21368/, nativeEvents: true, requireWindowFocus: true}, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: a544c8b8-dd2d-4e48-86f7-724945b6f508
*** Element info: {Using=id, value=userid}
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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:417)
at org.openqa.selenium.By$ById.findElement(By.java:218)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at com.app.test.AppLoginPolicySearch$1.apply(AppLoginPolicySearch.java:224)
at com.app.test.AppLoginPolicySearch$1.apply(AppLoginPolicySearch.java:1)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)
at com.app.test.AppLoginPolicySearch.isLoaded(AppLoginPolicySearch.java:220)
at com.app.test.AppLoginPolicySearch.testAppLoginPolicySearch(AppLoginPolicySearch.java:84)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)