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

时间:2012-06-06 09:53:59

标签: selenium continuous-integration selenium-webdriver specflow selenium-chromedriver

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

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

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

之前是否有人遇到此问题并找到修复程序?我们需要一个使用 HtmlUnitDriver的驱动程序的解决方案。

额外信息:

  • 语言= C#
  • 服务器= Windows Server 2008 R2
  • CI = TeamCity

编辑: Webdriver的设置是通过确保它尚未创建,然后创建ChromeDriver的新实例。伪/真实代码示例下面显示了它的设置,抱歉我不能显示完整的代码,因为我们使用其他选项,我们坚持使用(例如zap或fiddler集成/语言更改等)。

设置

[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;
}

2 个答案:

答案 0 :(得分:1)

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

我在我的setUp中有这个:

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

isRunningByCommandLineContents:

If WScript.Arguments.Count = 1 Then

strCmdLine = WScript.Arguments.Item(0)
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
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

killProcessByCommandLineContents:

If WScript.Arguments.Count = 1 Then
strProcess = WScript.Arguments.Item(0)
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
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);
    }
}

答案 1 :(得分:0)

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

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