For this document, we provide an example test located in our JUnit Github Repository.
JUnit is a Java unit testing framework that is useful for creating scalable and repeatable tests. Eclipse is a widely used Java integrated development environment (IDE) containing a comprehensive workspace for user's code development needs.
In this guide we will use JUnit with Eclipse for testing using the Selenium Webdriver and Java programming language.
- Download and install Eclipse IDE for Java Developers
- After installing is complete, launch Eclipse
- Select Create a new Java project
- Download Selenium WebDriver for Java
- Add the Selenium jars to the build path of your project
- Create a new JUnit Test Case
- Copying the following content:
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.*; import org.openqa.selenium.remote.*; import java.net.URL; import java.util.concurrent.TimeUnit;
class BasicExample {
private WebDriver driver; @BeforeEach void setUp() throws Exception { String username = "YOUR_USERNAME"; String authkey = "YOUR_AUTHKEY"; DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("version", "76"); capabilities.setCapability("platform", "WINDOWS"); capabilities.setCapability("name", "Junit Example"); capabilities.setCapability("browserName", "Chrome"); capabilities.setCapability("record_video", true); String hubAddress = String.format("http://%s:%[email protected]:80/wd/hub", username, authkey); URL url = new URL(hubAddress); driver = new RemoteWebDriver(url, capabilities); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @AfterEach void tearDown() throws Exception { driver.quit(); } @Test void test() { driver.get("http://crossbrowsertesting.github.io/selenium_example_page.html"); assertEquals("Selenium Test Example Page", driver.getTitle()); }
}
- Run your test by selecting the Run button
Congratulations! You have successfully integrated CBT and JUnit using Eclipse. Now you are ready to see your build start to run in the Crossbrowsertesting app.
By following the steps outlined in this guide, you are now able to seamlessly integrate JUnit and CrossBrowserTesting. If you have any questions or concerns, please feel free to reach out to our support team.