How to run Selenium tests using TestNG framework a

2019-03-01 03:02发布

问题:

I want to run Selenium tests written in TestNG framework using Jenkins.

The command configured in jenkins job (Freestyle project):

java -cp J:\taf\testng\*;J:\taf\workspace\TestNGExamples\bin;J:\automation\* org.testng.TestNG J:\taf\workspace\TestNGExamples\testng1.xml

Note: for -cp argument, given the directory paths to testng jar, test classes, and selenium jar

When I run the job (Build Now option), it is getting struck on create session activity of Selenium and giving the error.

Note:

The same command ran successfully when ran from the command line.

Error Log from Console Output: (jenkins)

Started by user Naveen
Building in workspace C:\Program Files (x86)\Jenkins\workspace\TestNG Example
[TestNG Example] $ cmd /c call C:\Windows\TEMP\hudson2261889395366550414.bat

C:\Program Files (x86)\Jenkins\workspace\TestNG Example>java -cp J:\taf\testng\*;J:\taf\workspace\TestNGExamples\bin;J:\automation\* org.testng.TestNG J:\taf\workspace\TestNGExamples\testng1.xml 
[TestNG] Running:
  J:\taf\workspace\TestNGExamples\testng1.xml

1481801204206   geckodriver INFO    Listening on 127.0.0.1:1387
Dec 15, 2016 4:56:44 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
1481801204827   mozprofile::profile INFO    Using profile path C:\Windows\TEMP\rust_mozprofile.KDz9DWYpWQAM
1481801204829   geckodriver::marionette INFO    Starting browser C:\Program Files\Mozilla Firefox\firefox.exe
1481801204831   geckodriver::marionette INFO    Connecting to Marionette on localhost:7795
1481801206090   Marionette  INFO    Listening on port 7795

###!!! [Parent][MessageChannel] Error: (msgtype=0x2E007D,name=PBrowser::Msg_Destroy) Channel error: cannot send/recv

JavaScript error: resource://app/modules/ContentCrashHandlers.jsm, line 75: TypeError: browser.permanentKey is not a non-null object

###!!! [Parent][MessageChannel] Error: (msgtype=0x2E007D,name=PBrowser::Msg_Destroy) Channel error: cannot send/recv

Dec 15, 2016 4:56:47 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C

###!!! [Parent][MessageChannel] Error: (msgtype=0x2E007D,name=PBrowser::Msg_Destroy) Channel error: cannot send/recv

Following is the Test Class:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

    public class SeleniumTests {

        @Test
        public void test1(){
            WebDriver driver = new FirefoxDriver();
            driver.get("http://www.google.co.in");
            driver.quit();
        }

    }

Following is the testng1.xml:

<?xml version="1.0" encoding="UTF-8"?>

<suite name="Default suite">
  <test verbose="2" name="Default test">
    <classes>
      <class name="SeleniumTests"/>
    </classes>
  </test> <!-- Default test -->
</suite> <!-- Default suite -->

Environment Details:

  1. Jenkins - 2.19.4 (as Windows Service)
  2. Java - 1.8
  3. TestNG - 6.8.5
  4. Selenium - 3.0.1
  5. Firefox - 51
  6. geckodriver - 0.11.1
  7. Os - Windows 7 - 64 bit

回答1:

This is not TestNG or Selenium issue. This error sometimes occure when Jenkins is insalled as Windows Service. Install Jenkins inside servlet container like Tomcat or GlassFish and it should help.



回答2:

As suggested by Mackowski, installed Jenkins in tomcat as a webapp (instead of Windows Service) and it solved the issue.

Reference:

  1. https://wiki.jenkins-ci.org/display/JENKINS/Tomcat

Note: When we run the tests when deployed Jenkins as Windows Service, no browser GUI is launched/observed. Jenkins inside tomcat, browser GUI is launched.