通过selenium网格运行脚本时出现NullPointerException

时间:2014-07-28 07:03:10

标签: java selenium-webdriver selenium-grid

我通过selenium网格并行运行12个测试用例(4个就是4个on firefox,4个用chrome)。在每次运行中,至少有3个投掷nullpointerexception(每次都不是相同的测试)。这是堆栈跟踪:

java.lang.NullPointerException
    at demo.CommonFunctions.captureScreenshot(Unknown Source)
    at demo.ExistingUserLogin.setUp(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:37)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
    at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

我也包括设置方法代码:

@BeforeClass
@Parameters({"browserName", "baseUrl", "sServerHost", "iServerPort", "KAPxuserName", "OLuserName", "MWOuserName",
    "KLIuserName", "Password", "fromAddr", "ToAddr", "EmailPassword", "screenshot_Path"})
public void setUp(String browserName, String baseUrl, String sServerHost, String iServerPort, String KAPxuserName,
    String OLuserName, String MWOuserName, String KLIuserName, String Password, String fromAddr, String toAddr,
    String EmailPassword, String screenshot_Path) throws Exception {
    try {
        Reporter.log("<<<Begin setup()>>>");
        ExistingUserLogin.baseUrl = baseUrl;
        ExistingUserLogin.browserName = browserName;
        ExistingUserLogin.sServerHost = sServerHost;
        ExistingUserLogin.iServerPort = iServerPort;
        ExistingUserLogin.KAPxuserName = KAPxuserName;
        ExistingUserLogin.OLuserName = OLuserName;
        ExistingUserLogin.MWOuserName = MWOuserName;
        ExistingUserLogin.KLIuserName = KLIuserName;
        ExistingUserLogin.password = Password;
        ExistingUserLogin.To_Addr = toAddr;
        ExistingUserLogin.From_Addr = fromAddr;
        ExistingUserLogin.EmailPassword = EmailPassword;
        ExistingUserLogin.ScreenShot_Path = screenshot_Path;

        DesiredCapabilities capability = new DesiredCapabilities();
        if (browserName.toLowerCase().contains("firefox".toLowerCase()))
            capability = DesiredCapabilities.firefox();
        else if (browserName.toLowerCase().contains("chrome".toLowerCase()))
            capability = DesiredCapabilities.chrome();

        else if (browserName.toLowerCase().contains("iexplore".toLowerCase())) {
            capability = DesiredCapabilities.internetExplorer();
            capability.setBrowserName("iexplore");
            capability.setPlatform(Platform.WINDOWS);
            capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
            capability.setCapability("initialBrowserUrl", baseUrl);
            capability.setJavascriptEnabled(true);
        }
        else if (browserName.toLowerCase().contains("safari".toLowerCase()))
            capability = DesiredCapabilities.safari();

        driver = new RemoteWebDriver(
            new URL("http://".concat(sServerHost).concat(":").concat(iServerPort).concat("/wd/hub")), capability);

        driver.get(baseUrl);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        // driver.manage().window().maximize();
        driver.manage().window().setPosition(new Point(0, 0));
        driver.manage().window().setSize(new Dimension(1500, 1000));
        CommonFunctions.deleteVisibleCookie(driver);
        Reporter.log("<<<End setup()>>>");
    }
    catch (Exception e) {
        String filename = CommonFunctions.captureScreenshot(driver, "SCR");
        new SendMail(From_Addr, To_Addr, EmailPassword, filename, e);
    }
}

这只发生在并行运行中,连续运行正常。

0 个答案:

没有答案