当驱动程序启动浏览器时,它始终与本地主机一起打开 然后导航到给定的URL但驱动程序没有获取当前页面 标题而不是总是显示本地主机标题"此页面不能 显示&#34 ;.我发布了我的代码和错误,请告诉我 哪里我错了。
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class TestIEBrowser {
公共WebDriver驱动程序;
@BeforeClass
public void setUp() {
System.out.println("*******************");
System.out.println("launching IE browser");
System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer_Win32_2.39.0\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
}
@Test(priority=1)
public void testGooglePageTitleInIEBrowser() {
driver.navigate().to("http://www.google.com");
driver.manage().timeouts().implicitlyWait( 30, TimeUnit.SECONDS );
String strPageTitle = driver.getTitle();
System.out.println("Page title: - "+strPageTitle);
Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"),
"Page `title doesn't match");`
}
@AfterClass
public void tearDown() {
if(driver!=null) {
System.out.println("Closing IE browser");
driver.quit();
}
} }
Error
launching IE browser
Started InternetExplorerDriver server (32-bit) 2.39.0.0
Listening on port 47579
Jul 31, 2017 5:43:08 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Page title: - This page can’t be displayed
Closing IE browser
FAILED: testGooglePageTitleInIEBrowser
java.lang.AssertionError: Page title doesn't match expected [true] but found [false]
at org.testng.Assert.fail(Assert.java:93)
at org.testng.Assert.failNotEquals(Assert.java:512)
at org.testng.Assert.assertTrue(Assert.java:41)
at TestIEBrowser.testGooglePageTitleInIEBrowser(TestIEBrowser.java:30)
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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
答案 0 :(得分:0)
尝试使用driver.get()而不是driver.navigate()。
您也可以尝试使用
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(title.isDisplayed)