使用IE

时间:2017-04-21 08:12:43

标签: selenium

有人可以帮忙解决这个问题吗

 package IEProjects;

    import java.io.File;

    import org.junit.BeforeClass;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.testng.Assert;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.Test;

    public class TestIEBrowser {


              static String driverPath = "IE driver path";
              public WebDriver driver;             


                @BeforeClass
                public void setUp() {
                    System.out.println("my IE");
                    System.out.println("launching IE browser");
                    System.setProperty("webdriver.ie.driver","C:\\IEDriverServer_Win32_3.3.0\\IEDriverServer.exe");

                    driver = new InternetExplorerDriver();
                    driver.manage().window().maximize();
                }

                @Test
                public void testGooglePageTitleInIEBrowser() {
                    driver.navigate().to("http://www.google.com");
                    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();
        }

    }
    }

以下是错误,感谢您的快速回复。

  

失败:testGooglePageTitleInIEBrowser       显示java.lang.NullPointerException         在IEProjects.TestIEBrowser.testGooglePageTitleInIEBrowser(TestIEBrowser.java:32)         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)         在org.testng.TestRunner.privateRun(TestRunner.java:744)         在org.testng.TestRunner.run(TestRunner.java:602)         在org.testng.SuiteRunner.runTest(SuiteRunner.java:380)         在org.testng.SuiteRunner.run顺序(SuiteRunner.java:375)         在org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)         在org.testng.SuiteRunner.run(SuiteRunner.java:289)         在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)         在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)         at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)         在org.testng.TestNG.runSuitesLocally(TestNG.java:1226)         在org.testng.TestNG.runSuites(TestNG.java:1144)         在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)         在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

谢谢

2 个答案:

答案 0 :(得分:2)

根本原因是因为未正确初始化驱动程序。

我认为您从Junit而不是testNG错误地导入beforeClass注释,从而导致未调用setUp方法并导致驱动程序未初始化

请将导入更改为import org.testng.annotations.BeforeClass;

答案 1 :(得分:1)

由于注释错误,IE浏览器无法启动,其中您使用了JUNIT注释而不是TestNg。