Site icon Vinsguru

Selenium Docker Integration Through Jenkinsfile – Part 1 – Setting Up Tests & Dependencies

Overview:

We already have seen how to create a disposable selenium grid at run time using Docker.

In this article, We are going to see how to run our automated tests inside docker container & what kind of benefits it brings us!

Udemy – Selenium WebDriver With Docker:

TestAutomationGuru has released a brand new course in Udemy on Selenium WebDriver with Docker. 14 hours course which starts with installing docker from scratch and goes all the way up to running dockerized selenium tests on AWS cloud. Please access the above link which gives you the special discount.  You can also get your money back if you do not like the course within 30 days.

Note:

We already have few posts on running automated tests inside Docker container. But those are specifically for a Java-Maven projects with Maven-Docker plugin.

Then, why are we creating another post with same content?

Well… As part of this article, we would be using the same Java/Maven framework as we used in the article above. But we would not use Maven plugin to create Docker image. Instead we would use Jenkins declarative pipeline to create image and push it to Docker Hub. If you are NOT using Jenkins, I would suggest you to follow the above approaches & skip this series!

This approach would be better as it completely separates Docker image build process from Maven. It could be useful for any functional test automation project. Not specific to Java.

Even if you are not a Java user, I would still encourage you to continue reading this article to get some idea. Part 2/Part 3 of this series are very generic. Not Java Specific. So, I hope you would get good understanding.

For the sake of creating a separate series, I am shamelessly copying some content from the above article just for this article.

Challenges In Test Execution:

We already have simplified our selenium grid set up using docker using the above articles. But we still face few challenges. Your tests still have few dependencies to run them!

Managing these dependencies & Setting up remote slaves are time-consuming for big applications which has thousands of test cases to run on a daily basis on a multiple slave machines.

That too, if you are planning to move AWS /cloud in future, How can you set this up all programmatically?

We will see how docker could help us here!!

Sample Test Framework:

I have uploaded a sample project here in GitHub. You could fork this project in Github or use your own project in Java/Javascript/Python etc.

Page Objects & Tests:

public class BaseTest {

    protected WebDriver driver;
    
    @BeforeTest
    public void setUp() throws MalformedURLException {
        
        DesiredCapabilities dc = DesiredCapabilities.chrome();

        if (System.getProperty("browser").equals("firefox"))
            dc = DesiredCapabilities.firefox();

        String host = System.getProperty("seleniumHubHost");
        
        driver = new RemoteWebDriver(new URL("http://" + host + ":4444/wd/hub"), dc);
        
    }

    @AfterTest
    public void tearDown() throws InterruptedException {
        driver.quit();
    }  
}

Test Suites:

The project contains 2 suite files. Lets assume that each suite is for testing specific business module of the application. Here in our case,

Building Project Locally:

mvn clean package -DskipTests
cd target
java -cp container-test.jar:libs/* -DseleniumHubHost=10.11.12.13 -Dbrowser=chrome org.testng.TestNG ../suite/search-module.xml

Test Results:

When you run the above test using command line, make a note of the result files it creates. TestNG by default creates a test-output directory under project root directory and stores all the results files there. If your are using a different framework, ensure that you create a separate directory for your results. We might need this info later in this series.

Summary:

We created a simple project with our page objects, tests and test suites. We built the project through build tools (here it is Maven) & ensured that it works fine through command line.

Once you have a working project, list the dependencies you would need to run the project in a completely new environment. We would need those to create our Dockerfile which will be covered in the next article.

Happy Testing & Subscribe 🙂

 

Share This:

Exit mobile version