当前位置: 首页 > 知识库问答 >
问题:

Selenium网格并行执行线程本地WebDriver会话不创建异常:无法创建新服务:GeckoDriverService

孟修竹
2023-03-14

使用selenium grid在windows节点和mac节点上并行运行测试将通过所有测试,但无法运行并跳过mac Firefox浏览器或windows Firefox浏览器的第一个测试过滤器MoreResultsBySquarefeet。它不断抛出这个错误。openqa。硒。SessionNotCreatedException:无法创建新服务:GeckoDriverService我正在使用最新的java、最新的selenium驱动程序、最新的浏览器驱动程序和最新的浏览器版本。我在启动json selenium网格文件的同一文件夹中有所有驱动程序,并且我的代码系统属性指向同一文件夹。我还在windows环境变量中的路径和mac上的路径中设置了文件夹。有人遇到过这个错误吗?你是如何解决的?

  public class TestBase {
       private static ThreadLocal<WebDriver> driverThread = new ThreadLocal<>();

    public static String whichNodeURL = "";
    public static String nodeURL = "http://xxxx:4444/wd/hub";
    public static String macNodeURL = "http://xxxx:5555/wd/hub";
    public static String winNodeURL = "http://xxxx:5554/wd/hub";

    DesiredCapabilities capabilities = new DesiredCapabilities();

    @SuppressWarnings({ "rawtypes"})
    @BeforeMethod (alwaysRun=true)
    //Use before method instead of before class or before test so each method/test will open in new browser; 
    //This was tested and found before method was the only one that works.
        @Parameters("browser")
    public final void setDriver(String browser) throws IOException, InterruptedException{

            ReadProperties.retrieveGlobalProperties();

        if (ReadProperties.globalProp.getProperty("webautomation").contains("yes") && ReadProperties.globalProp.getProperty("mobileautomation").contains("no"))
        {
            if(browser.contains("winfirefox"))
            {

                System.setProperty("webdriver.gecko.driver", ReadProperties.globalProp.getProperty("pcgeckodriver"));
                FirefoxOptions firefoxOptions = new FirefoxOptions();
                firefoxOptions.setCapability("platform", "WINDOWS");
                firefoxOptions.setCapability("browser", "firefox");
                firefoxOptions.setCapability("newCommandTimeout", 5000);
                firefoxOptions.setCapability(FirefoxDriver.MARIONETTE, true);
                firefoxOptions.setBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe");    
                whichNodeURL=winNodeURL;    
                try
                {
                    driverThread.set(new RemoteWebDriver(new URL(whichNodeURL), firefoxOptions));
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }


            }
            else if(browser.contains("macfirefox"))
            {
                System.setProperty("webdriver.gecko.driver", ReadProperties.globalProp.getProperty("macgeckodriver"));
                FirefoxOptions macfirefoxOptions = new FirefoxOptions();
                macfirefoxOptions.setCapability("platform", "MAC");
                macfirefoxOptions.setCapability("browser", "firefox");
                macfirefoxOptions.setCapability("newCommandTimeout", 5000);
                macfirefoxOptions.setCapability(FirefoxDriver.MARIONETTE, true);
                macfirefoxOptions.setBinary("/Applications/Firefox.app/Contents/MacOS/firefox-bin");
                whichNodeURL=macNodeURL;

                try
                {
                    driverThread.set(new RemoteWebDriver(new URL(whichNodeURL), macfirefoxOptions));
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }


            }
            else if (browser.contains("winchrome"))
            {
                System.setProperty("webdriver.chrome.driver", ReadProperties.globalProp.getProperty("pcchromedriver"));
                ChromeOptions chromeOptions = new ChromeOptions();
                chromeOptions.setCapability("platform", "WINDOWS");
                chromeOptions.setCapability("browser", "chrome");
                chromeOptions.setCapability("newCommandTimeout", 5000);
                whichNodeURL=winNodeURL;

                try
                {
                    driverThread.set(new RemoteWebDriver(new URL(whichNodeURL), chromeOptions));
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

            }
            else if (browser.contains("macchrome"))
            {
                System.setProperty("webdriver.chrome.driver", ReadProperties.globalProp.getProperty("macchromedriver"));
                ChromeOptions macchromeOptions = new ChromeOptions();
                macchromeOptions.setCapability("platform", "MAC");
                macchromeOptions.setCapability("browser", "chrome");
                macchromeOptions.setCapability("newCommandTimeout", 5000);
                whichNodeURL=macNodeURL;

                try
                {
                    driverThread.set(new RemoteWebDriver(new URL(whichNodeURL), macchromeOptions));
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

            }
                String weburl = ReadProperties.globalProp.getProperty("weburl");
                this.driverThread.get().get(weburl);
                Thread.sleep(1000);
                this.driverThread.get().manage().window().maximize(); 
                Thread.sleep(1000);
           }
      }
    }
        public static WebDriver getDriver() {
        return driverThread.get();
    }
        @AfterMethod(alwaysRun=true) 
    public static void OnFailure(ITestResult testResult) throws IOException { 
        if (testResult.getStatus() == ITestResult.FAILURE) 
        { 
            System.out.println(testResult.getStatus()); 
        } 
    } 


@AfterMethod(alwaysRun=true)
//Use after method instead of after class or after test so each method/test will open in new browser; 
//This was tested and found after method was the only one that works.   
public void tearDown() {

        getDriver().quit();
}
}






  public class FilterMoreResultsBySquareFeet extends TestBase{

 static SoftAssert softAssert = new SoftAssert();
 final static Logger log = 
 LogManager.getLogger(FilterMoreResultsBySquareFeet.class);


static String className = 
FilterMoreResultsBySquareFeet.class.getSimpleName();
static Date date1= new Date();
static String originaltimestamp = new Timestamp(date1.getTime()).toString();
static String timestamp = originaltimestamp.replace(':', 'x').substring(11);
static String foldername = className+timestamp;
static String errorname = "";

  @Parameters("browser")
@Test(groups= {"smoke", "regression"}, dataProvider = "getData") 
public void filterResultsBySqFeet (String searchkeyword, String minsqfeet, String maxsqfeet) throws IOException, InterruptedException
{
    WebDriver webdriver = getDriver();
    Search.searchByCity(webdriver, searchkeyword);
    FilterMoreResults_Page.clickOpenMoreFilters(webdriver);
    FilterMoreResults_Page.filterBySqFeet(webdriver, minsqfeet, maxsqfeet);
    FilterMoreResults_Page.applyMoreFilters(webdriver);
    String diditfilter = FilterMoreResults_Page.verifyFilterBySqFeet(webdriver, minsqfeet, maxsqfeet);

    try{
        Assert.assertEquals(diditfilter, "yes");
    } 
    catch(AssertionError e)
    { 
        log.error("Didn't filter by square feet.", e.getMessage());
        errorname = "didntfilterbysqft";
        ScreenshotURL.screenshotURL(webdriver, foldername, errorname);
        softAssert.fail();
    }

       softAssert.assertAll();

}


  TESTNG FILE
  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="XOME Suite" parallel="tests" verbose="10" thread-count="2">

<test name = "Win Firefox Tests" preserve-order="true" group-by-instances="true">
<parameter name="browser" value="winfirefox">
    <classes>
    <class name="webTests.FilterMoreResultsBySquareFeet" />
        <class name="webTests.FilterMoreResultsByKeyword" />
        <class name="webTests.FilterMoreResultsByYear" />
    </classes>
</parameter>
</test>

<test name = "Mac Firefox Tests" preserve-order="true" group-by-instances="true">
<parameter name="browser" value="macfirefox">
    <classes>
    <class name="webTests.FilterMoreResultsBySquareFeet" />
        <class name="webTests.FilterMoreResultsByKeyword" />
        <class name="webTests.FilterMoreResultsByYear" />
    </classes>
</parameter>
</test>

<test name = "Win Chrome Tests" preserve-order="true" group-by-instances="true">
<parameter name="browser" value="winchrome">
    <classes>
    <class name="webTests.FilterMoreResultsBySquareFeet" />
        <class name="webTests.FilterMoreResultsByKeyword" />
        <class name="webTests.FilterMoreResultsByYear" />
    </classes>
</parameter>
</test>
<test name = "Mac Chrome Tests" preserve-order="true" group-by-instances="true">
<parameter name="browser" value="macchrome">
    <classes>
    <class name="webTests.FilterMoreResultsBySquareFeet" />
        <class name="webTests.FilterMoreResultsByKeyword" />
    <class name="webTests.FilterMoreResultsByYear" />
    </classes>
</parameter>
</test>
 macnode.json
 {
  "capabilities":
 [
  {
  "browserName": "firefox",
  "marionette": true,
  "maxInstances": 1,
  "version": 66,
  "platform": "MAC",
  "seleniumProtocol": "WebDriver"
},
{
  "browserName": "chrome",
  "maxInstances": 1,
  "version": 73,
  "platform": "MAC",
  "seleniumProtocol": "WebDriver"
},
{
  "browserName": "safari",
  "technologyPreview": false,
  "platform": "MAC",
  "maxInstances": 1,
  "seleniumProtocol": "WebDriver"
}
],
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 1,
"port": 5555,
"register": true,
"registerCycle": 5000,
"hub": "http://xxxx:4444",
"nodeStatusCheckTimeout": 5000,
"nodePolling": 5000,
"role": "node",
"cleanUpCycle": 5000,
"timeout": 5000,
"unregisterIfStillDownAfter": 60000,
"downPollingLimit": 2,
"debug": false,
"servlets" : [],
"withoutServlets": [],
"custom": {}
}

hub.json
{
"host": null,
"port": 4444,
"newSessionWaitTimeout": -1,
"servlets" : [],
"prioritizer": null,
"capabilityMatcher": 
"org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
"throwOnCapabilityNotPresent": true,
"nodePolling": 5000,
"cleanUpCycle": 5000,
"timeout": 300000,
"browserTimeout": 0,
"maxSession": 1
}

java-jarselenium-server-standalone-3.141。59.jar-role hub-hubConfig hub。json

java-Dwebdriver。铬。driver=“C:\\seleniumgrid\\chromedriver.exe”-Dwebdriver。壁虎。driver=“C:\\seleniumgrid\\geckodriver.exe”-Dwebdriver。ie.driver=“C:\\seleniumgrid\\IEDriverServer.exe”-Dwebdriver。边driver=“C:\\seleniumgrid\\MicrosoftWebDriver.exe”-jar C:\\seleniumgrid\\selenium-server-standalone-3.141。59.jar-角色节点-节点图C:\\seleniumgrid\\WindowsNode。json

java-Dwebdriver。铬。driver=“/Users/abc/seleniumgrid/chromedriver”-Dwebdriver。壁虎。driver=“/Users/abc/seleniumgrid/geckodriver”-jar/Users/abc/seleniumgrid/selenium-server-standalone-3.141。59.jar-role节点-nodeConfig/Users/abc/seleniumgrid/MacNode。json

org.openqa.selenium.SessionNotCreatedException: Unable to create new service: GeckoDriverService

[错误]测试运行: 10,失败: 2,错误: 0,跳过: 1,时间流逝: 287.762 s

共有1个答案

濮阳振
2023-03-14
Ok after struggling with it for several days and trying everything, I got it to work. Hope this helps someone too. There wasn't much info out there on solving this error and the error didn't tell me much where it was wrong.
Here's what I changed.
1. Use the hub url for creating your remotewebdriver for each browser and do not use the node url.
    public static String nodeURL = "http://xxxx:4444/wd/hub";
    public static String macNodeURL = "http://xxxx:4444/wd/hub";
    public static String winNodeURL = "http://xxxx:4444/wd/hub";
2. Add this to the node json files 

        {
      "browserName": "firefox",
      "marionette": true,
      "maxInstances": 1,
      "version": 66,
      "platform": "WINDOWS",
      "seleniumProtocol": "WebDriver",
      "webdriver.gecko.driver": "C:/seleniumgrid/geckodriver.exe",
      "binary":"C:/Program Files/Mozilla Firefox/firefox.exe"
    },

        {
      "browserName": "firefox",
      "marionette": true,
      "maxInstances": 1,
      "version": 66,
      "platform": "MAC",
      "seleniumProtocol": "WebDriver",
      "webdriver.gecko.driver": "/Users/angee/seleniumgrid/geckodriver",
      "binary": "/Applications/Firefox.app/Contents/MacOS/firefox-bin"
    },


3. Set this in your code 
firefoxOptions.setBinary("C:/Program Files/Mozilla Firefox/firefox.exe");
                    macfirefoxOptions.setBinary("/Applications/Firefox.app/Contents/MacOS/firefox-bin");
 类似资料:
  • null 我的脚本中使用的代码: 详细的堆栈跟踪: an 19,2017 6:07:36 PM org.openqa.selenium.remote.ProtocolHandshake创建session信息:尝试双方言会话,假设Postel定律在远程端成立,Jan 19,2017 6:07:37 PM org.openqa.selenium.remote.ProtocolHandshake创建se

  • org.openqa.selenium.SessionNotCreatedException:无法创建新会话。(原始错误:命令失败:C:\Windows\system32\cmd.exe/s/C“C:\Program Files(x86)\Android\Android sdk\platform tools\adb.exe”-s 69c7aa170104安装“C:\Program Files(x8

  • 直到昨天:以下代码工作正常。 但是今天早上它开始抛出:异常线程"main"org.openqa.selenium.SessionNotCreatedException:会话未创建异常 我在Stackoverflow中阅读了几个答案。我知道如果selenium web驱动程序和chrome驱动程序之间存在版本不匹配,就会引发此错误。所以我下载了最新的Selenium Web驱动程序(3.0.1 fo

  • 这是因为我在一个搜索字段上有一个失败的Behat测试-这个字段是通过javascript添加的,所以我假设Selenium(与我的设置)有问题。 我发现,如果我访问http://localhost:4444/wd/hub并单击“create Session”,选择任何浏览器(android、iphone、firefox、chrome、internet explorer、opera),我将得到错误消

  • 我就是这么开始的:Hub命令:Java-jar selenium-server-standalone-3 . 141 . 59 . jar-role Hub节点命令:Java-dwebdriver . chrome . driver = " \ Users \ rachitamital \ eclipse-workspace \ first javaproject \ drivers \ chro

  • 它的发生是因为我创建了许多线程,而没有关闭它们?还是经常创造新的? 有人能告诉我在代码中是否做错了什么吗?