Selenium wedriver:并行执行将引发'org.openqa.selenium.NoSuchSessionException:无效的会话ID'

时间:2018-09-28 17:56:56

标签: java multithreading selenium-webdriver testng thread-local

我正在尝试并行执行测试用例。当我尝试在远程计算机(仅限我的本地主机)上的浏览器(chrome或firefox或两者)上超过两个以上的测试用例时,抛出以下错误。

*主要基础* : 公共类TestBase {

public ThreadLocal<RemoteWebDriver> tlDriver = new ThreadLocal<RemoteWebDriver>();
public static ExcelTestData dataTable = new ExcelTestData();
public static Properties config = new Properties();
public static FileInputStream fis;
public static RemoteWebDriver driver;
public static BasicFunctions basics = new BasicFunctions(); 
public static ExtentReports extentReport = ExtentReportManager.getInstance();
public static ExtentTest extentTest;

}

*函数库** 公共类BasicFunctions扩展TestBase {

公共无效寄存器()     {

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(ObjectRepositories.xpath_registerLink)));
    driver.findElement(By.xpath(ObjectRepositories.xpath_registerLink)).click();
    if(driver.findElement(By.xpath(ObjectRepositories.xpath_registrationTitle)).isDisplayed())
    {           
        driver.findElement(By.xpath(ObjectRepositories.xpath_loginTextbox)).sendKeys(dataTable.getData("RegressionData", "Register_login"));
        driver.findElement(By.xpath(ObjectRepositories.xpath_emailTextbox)).sendKeys(dataTable.getData("RegressionData", "Register_email"));
        driver.findElement(By.xpath(ObjectRepositories.xpath_reg_passwordTextbox)).sendKeys(dataTable.getData("RegressionData", "Register_password"));
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(ObjectRepositories.xpath_reg_conf_passwordTextbox)));
        driver.findElement(By.xpath(ObjectRepositories.xpath_reg_conf_passwordTextbox)).sendKeys(dataTable.getData("RegressionData", "Register_confirmPassword"));
        sleep(2000);
        try
        {
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(ObjectRepositories.xpath_reg_submisson)));
        driver.findElement(By.xpath(ObjectRepositories.xpath_reg_submisson)).click();
        }
        catch(NoSuchElementException nsee)
        {
            driver.findElement(By.xpath(ObjectRepositories.xpath_reg_passwordTextbox)).click();
        }
    }
}

公共同步的void driverInititation(字符串浏览器)     {

    System.out.println("Thread name is "+ Thread.currentThread().getName() + " and Thread ID is "+ Thread.currentThread().getId());
    if (ExcelReader.gridExecution.equals("Yes"))
    {
        if (Browser.equalsIgnoreCase("Firefox"))
        {
            System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"/src/test/resources/Executables/geckodriver.exe");
            DesiredCapabilities cap = DesiredCapabilities.firefox();
            try
            {
                tlDriver.set(new RemoteWebDriver(new URL(config.getProperty("Grid_Url")+":"+config.getProperty("Port_Firefox")+"/wd/hub"),cap));
                driver = tlDriver.get();
            }
            catch(MalformedURLException urlex)
            {
                urlex.printStackTrace();
            }
            catch(Exception e)
            {
                System.out.println("Other exception");
                e.printStackTrace();
            }
        }
        else if (Browser.equalsIgnoreCase("Chrome"))
        {
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/src/test/resources/Executables/chromedriver.exe");
            DesiredCapabilities cap = DesiredCapabilities.chrome();
            try
            {
                tlDriver.set(new RemoteWebDriver(new URL(config.getProperty("Grid_Url")+":"+config.getProperty("Port_Chrome")+"/wd/hub"),cap));
                driver = tlDriver.get();
            }
            catch(MalformedURLException urlex)
            {
                urlex.printStackTrace();
            }
            catch(Exception e)
            {
                System.out.println("Other exception");
                e.printStackTrace();
            }
        }
    }
    else
    {
        if(Browser.equalsIgnoreCase("Firefox"))
        {
            System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"/src/test/resources/Executables/geckodriver.exe");
            driver= new FirefoxDriver();
        }
        else if(Browser.equalsIgnoreCase("Chrome"))
        {
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/src/test/resources/Executables/chromedriver.exe");
            driver= new ChromeDriver();
        }
    }
    if(driver!=null)
    {
        driver.get(config.getProperty("ApplicationUrl"));
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        wait = new WebDriverWait(driver,30);
        javascript = (JavascriptExecutor)driver;
    }
}

}

*我的测试用例* 公共类LoginValidation扩展TestBase {

//public WebDriver driver;
@Parameters({"Browser"})
@BeforeTest
public void setupInitiation(String Browser)
{
    Thread.currentThread().setName(this.getClass().getSimpleName());
    basics.driverInititation(Browser);
}

@Test
public void execution()
{
    basics.login();
    basics.logout();
}

}

例外:

org.openqa.selenium.NoSuchSessionException:无效的会话ID   (驱动程序信息:chromedriver = 2.42.591088(7b2b2dca23cca0862f674758c9a3933e685c27d5),platform = Windows NT 10.0.17134 x86_64)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:0毫秒 构建信息:版本:'3.14.0',修订版本:'aacccce0',时间:'2018-08-02T20:19:58.91Z' 系统信息:主机:'PRABU',ip:'192.168.92.2',操作系统名称:'Windows 8.1',os.arch:'amd64',os.version:'6.3',java.version:'1.8.0_40 ' 驱动程序信息:org.openqa.selenium.remote.RemoteWebDriver 功能{acceptInsecureCerts:false,acceptSslCerts:false,applicationCacheEnabled:false,browserConnectionEnabled:false,browserName:chrome,chrome:{chromedriverVersion:2.42.591088(7b2b2dca23cca0 ...,userDataDir:C:\ Users \ PRABUN〜1 \ AppData \ L ...},cssSelectorsEnabled:true,databaseEnabled:false,goog:chromeOptions:{debuggerAddress:localhost:50326},handlesAlerts:true,hasTouchScreen:false,javascriptEnabled:true,locationContextEnabled:true,mobileEmulationEnabled:false,nativeEvents:true,networkConnectionEnabled :false,pageLoadStrategy:正常,平台:XP,platformName:XP,可旋转:false,setWindowRect:true,takesHeapSnapshot:true,takesScreenshot:true,意外AlertBehaviour:,unhandledPromptBehavior:,版本:69.0.3497.100,webStorageEnabled:true,webdriver.remote .sessionid:f24126658364be46f247f15b580 ...} ***元素信息:{使用= xpath,值= // div / a [@href ='#/ register']}     在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处     在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)     在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     在java.lang.reflect.Constructor.newInstance(Constructor.java:422)     在org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)     在org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)     在org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)     在org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)     在org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)     在org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)     在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)     在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:322)     在org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:424)     在org.openqa.selenium.By $ ByXPath.findElement(By.java:353)     在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:314)     在com.servicenow.functionallibraries.BasicFunctions.register(BasicFunctions.java:98)     在com.servicenow.testcases.RegistrationFormEntry.execution(RegistrationFormEntry.java:30)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处     在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:497)     在org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)     在org.testng.internal.Invoker.invokeMethod(Invoker.java:580)     在org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)     在org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)     在org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)     在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)     在org.testng.TestRunner.privateRun(TestRunner.java:648)     在org.testng.TestRunner.run(TestRunner.java:505)     在org.testng.SuiteRunner.runTest(SuiteRunner.java:455)     在org.testng.SuiteRunner.access $ 000(SuiteRunner.java:40)     在org.testng.SuiteRunner $ SuiteWorker.run(SuiteRunner.java:489)     在org.testng.internal.thread.ThreadUtil $ 1.call(ThreadUtil.java:52)     在java.util.concurrent.FutureTask.run(FutureTask.java:266)     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:745)

0 个答案:

没有答案