我正在尝试创建一个简单的测试,只需打开页面并验证其标题即可。我认为这可能不简单,但是......我的测试在当前打开的Chrome浏览器中打开了一个新标签,但在driver.get(myUrl);
<{1}}上失败了org.openqa.selenium.NoSuchSessionException
我尝试升级和降级chromedriver并升级降级硒,但到目前为止没有运气。目前我在selenium-java 3.3.1,chromedriver 2.28和chrome 57.0.2987.98。任何帮助表示赞赏。
奖金问题,上次我用chromedriver打开了一个新的浏览器窗口,有什么方法可以回到那个?我更喜欢在新窗口中使用我的测试而不是新标签。
public class MyExampleTest {
private String baseUrl;
private WebDriver driver;
@Before
public void openBrowser() {
baseUrl = "http://myurl/";
System.setProperty("webdriver.chrome.driver", "C:\\Projects\\chromedriver.exe");
driver = new ChromeDriver();
driver.get(baseUrl);
}
@Test
public void pageTitleAfterSearchShouldBeThere() throws IOException {
assertEquals("The page title should be this at the start of the test.", "my title", driver.getTitle());
}
}
堆栈跟踪:
org.openqa.selenium.NoSuchSessionException: no such session
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 13 milliseconds
Build info: version: '3.3.1', revision: '5234b325d5', time: '2017-03-10 09:10:29 +0000'
System info: host: 'XXXXXXXXXXXXXXX', ip: 'xxx.xxx.xxx.xx', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{message=unknown error: Chrome failed to start: crashed
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64), platform=ANY}]
Session ID: 359ce80c1b82e53fffae055451228404
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
at ec_selenium.CapExampleTest.openBrowser(CapExampleTest.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.RunBefores.evaluate(RunBefores.java:24)
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:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
答案 0 :(得分:0)
你可能搞砸了imports
。试试下面的代码,对我来说很好。
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyExampleTest {
private String baseUrl;
private WebDriver driver;
@Before
public void openBrowser() {
baseUrl = "http://facebook.com/";
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\chromedriver.exe");
driver = new ChromeDriver();
driver.get(baseUrl);
driver.manage().window().maximize();
}
@Test
public void pageTitleAfterSearchShouldBeThere() throws IOException {
System.out.println(driver.getTitle());
assertEquals("The page title should be this at the start of the test.", "Facebook - Log In or Sign Up", driver.getTitle());
}
@After
public void closeBrowser(){
try{
driver.quit();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
每次都会打开new window
而不是已打开的Chrome浏览器中的新标签页。
答案 1 :(得分:0)
因为chromedriver版本和兼容的webdriver版本不匹配。 要么改变其中任何一个,要么同时采取最新的。这对我有用!