In today's fast-paced software development landscape, implementing Continuous Integration (CI) practices has become an absolute necessity. CI allows developers to integrate their code into a shared repository frequently, leading to rapid feedback and higher quality products. But how do we ensure that our web applications are bug-free and function correctly every time we integrate new code? This is where Selenium and Jenkins come into play.
Selenium is an open-source tool that allows developers to automate web browsers across various platforms. It provides a suite of tools that are used to drive browsers programmatically and validate their functionalities. It supports numerous programming languages, such as Java, C#, Python, and Ruby.
Jenkins is an open-source automation server designed to facilitate CI and CD (Continuous Deployment). With its robust ecosystem of plugins, Jenkins can automate various stages of the software development lifecycle, including building code, running tests, and deploying applications.
Integrating Selenium with Jenkins helps to automate the testing process, which can save valuable time and resources. The benefits are manifold:
Let’s walk through a step-by-step guide to set up a CI pipeline that incorporates Selenium tests with Jenkins.
First, create a Maven project for your Selenium tests. Your pom.xml
should have dependencies for Selenium and JUnit (for test cases) declared:
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
Create a test class in your project. Here’s a simple example using JUnit and Selenium:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class GoogleSearchTest { private WebDriver driver; @Before public void setUp() { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @Test public void testGoogleSearch() { driver.get("https://www.google.com"); String title = driver.getTitle(); Assert.assertEquals("Google", title); } @After public void tearDown() { if (driver != null) { driver.quit(); } } }
http://localhost:8080
.clean test
in the Goals field.After everything is set up, you can now trigger builds manually or through your defined triggers. Jenkins will pull the latest code from your repository, compile it, and execute the tests defined in your project.
By following these steps, you will have a fully functioning CI pipeline that automatically runs Selenium tests every time code is integrated into the repository.
21/09/2024 | UI Automation
18/09/2024 | UI Automation
18/09/2024 | UI Automation
18/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
18/09/2024 | UI Automation