Selenium2 - 在 CI 上运行测试会导致多个驱动程序实例



对于我们的BDD测试,我们使用与Selenium 2 webdriver(在本例中为Chrome驱动程序)通信的Specflow。

在本地主机上运行时(是的,"它在我的机器上工作"在对话中出现了几次),测试工作正常。他们设置数据和新的网络驱动程序,进行测试,然后拆除网络驱动程序和数据。即使测试因为我使用了正确的属性而出现严重错误,拆卸总是会被击中,因此driver.Quit()运行会破坏浏览器和驱动程序。

当我使用我们的持续集成[TeamCity]在我们的服务器[Windows Server 2008 r2]上运行它时,就会出现问题。由于某种原因,它将开始运行多个驱动程序实例,从而导致测试失败。

以前有没有人遇到过这个问题并找到了解决方法?我们需要一个使用驱动程序的解决方案 不HtmlUnitDriver .

额外信息:

  • 语言 = C#
  • 服务器 = Windows Server 2008 R2
  • CI = 团队城市

编辑:通过确保尚未创建 Web 驱动程序来设置 Web驱动程序,然后创建ChromeDriver的新实例。下面的伪/真实代码示例显示了它的设置方式,抱歉,我无法显示完整的代码,因为它有很多绒毛,因为我们用于我们坚持的其他选项(例如zap或小提琴手集成/语言更改等)。

设置

[SetUp]
[BeforeScenario()]
public static void BeforeWebScenario()
{
   if driver == null
     driver = new ChromeDriver();
   // Code to start page
}

[TearDown]
[AfterScenario()]
public static void AfterWebScenario()
{
   try
   {
       driver.Quit();
   } catch (Exception)
   {
       throw Exception
   }
   driver = null;
}

我也有这个问题。 我通过在我的testSetup()方法中杀死任何正在运行的chromedriver实例来修复它.exe。 我使用VBScript和一些Groovy代码来运行脚本。 对不起,这个答案有点长。

我的设置中有这个):

if (wshsc.isRunningByCommandLineContents("chromedriver.exe"))
    wshsc.killProcessByCommandLineContents("chromedriver.exe")

isRunningByCommandLineContent:

If WScript.Arguments.Count = 1 Then
strCmdLine = WScript.Arguments.Item(0)
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
Set objShell = CreateObject("WScript.Shell")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process")
If colProcessList.Count > 0 Then
    For Each objItem in colProcessList
        If (InStr(objItem.CommandLine, strCmdLine)) Then
            If (InStr(objItem.CommandLine, "cscript")) Then
            Else
                WScript.StdOut.Write "A process is running with " + strCmdLine + " in its command line = " + objItem.Name
            End If  
        End If
    Next
End If
End If

killProcessByCommandLineContent:

If WScript.Arguments.Count = 1 Then
strProcess = WScript.Arguments.Item(0)
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
Set objShell = CreateObject("WScript.Shell")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process")
If colProcessList.Count > 0 Then
    For Each objItem in colProcessList
        If InStr(objItem.CommandLine, strProcess) Then
            If (InStr(objItem.CommandLine, "cscript")) Then
            Else
                WScript.StdOut.Write objItem.Name + " "
                objItem.Terminate()
            End If
        End If
    Next
Else
    WScript.StdOut.Write "No instances found running"
End If
Else
WScript.StdOut.Write "Bad Arguments"
End If

以及"运行脚本部分":

public void killProcessByCommandLineContents(String contents) {
    List<String> arg = new ArrayList<String>()
    arg.add(contents)
    String [] args = arg.toArray()
    runScript("killByCmdLineContents.vbs", args, true)
}
public boolean isRunningByCommandLineContents(String contents) {
    List<String> arg = new ArrayList<String>()
    arg.add(contents)
    String [] args = arg.toArray()
    String returnData = runScript("IsRunningByCmdLineContents.vbs", args, true)
    if (returnData.contains(contents)) {
        return true
    } else {
        return false 
    }
}
public String runScript(String name, String [] args, boolean returnOutput) {
    String s = null;
    List<String> cmdLine = new ArrayList<String>()
    cmdLine.add("C://Windows//System32//cscript.exe")
    cmdLine.add(dir + "dir//src//com//misc//wshScripts//" + name)
    int index = 0
    args.each() {
        cmdLine.add(args[index])
        index++
    }
    try {
        String [] cmdLineArray = cmdLine.toArray()
        Process p = Runtime.getRuntime().exec(cmdLineArray, null);
        if (returnOutput) {
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String dataToReturn
            Log.logger.info("Standard output: ");
            while ((s = stdInput.readLine()) != null) {
                Log.logger.info(s)
                dataToReturn = s // should get last line
            }
            Log.logger.info("Standard error: ");
            while ((s = stdError.readLine()) != null) {Log.logger.info(s);}
            return dataToReturn
        } else {
            return ""
        }
    }
    catch (IOException e) {
        Log.logger.info(e.message, e);
    }
}
如果使用 DriverService

接口,请保留该服务,直到完成驱动程序,并调用 DriverService.stop()。对我来说,driver.quit() 还不够,因为我也在使用 DriverService。

driver.close();
driver.quit();
driverService.stop();

最新更新