Selenium is a framework designed for testing web applications. It can be used with a variety of popular programming languages, including C#, Java, PHP, Python, Ruby, etc. In this tutorial we will be using Selenium with Java to demonstrate a simple test suite on Google Search.
Installing Java
First, we have to set up our testing envrionment. This means that we have to install and setup our various technologies that allow us to run Selenium. The first item we will need to install is Java. We will do this by installing a Java Development Kit that includes the Java Runtime Envrionment. This is required to read and write new programs. You can download Java
here. After installing it, you should be able to type the command
java -version in the Terminal and get a response back that includes the details of the installed Java version. This lets us know that Java is currently installed on our machines and which version we are running.
Installing Eclipse
Next, we have to install Eclipse, which is our IDE, or integrated development environment. This is where we actually write our code and run it. You can download Eclipse
here.
Installing Maven
We also need to install Maven. Instructions on how to do this on a Windows machine are located
here.
Finally, we have to install
ChromeDriver. The link for that is
here.
ChromeDriver enables us to run automated tests using the Google Chrome browser. To install
ChromeDriver, follow the same steps you used to install Selenium and be sure that the
ChromeDriver executable is located in your PATH.
Setting up Selenium Project with Eclipse.
We now have to properly configure Eclipse to run Selenium. We do this by following these steps (For Windows machines):
1. Launch the "eclipse.exe" file inside the "eclipse" folder.
2. If you followed step 1 correctly, the executable should be located on C:\eclipse\eclipse.exe.
3. When asked to select for a workspace, just accept the default location. Create a new project through File > New > Other. Then start typing in the search option "Maven." Select "Maven Project."
4. Select the "Create a simple project (skip archetype selection)" option. You can give any name to the "GroupId" and "ArtifactId", just make sure that it is formatted with capital letters starting each word and no spaces. Otherwise you'll get an error message.
5. Click the drop down arrow under the current Maven project you are working in. Select the pom.xml file in the Package Explorer. This is where your dependencies that you are using are listed.
6. In your browser, navigate to
https://mvnrepository.com/
7. Search for "Selenium Java"
8. Select the latest version of Selenium and install it by copying and pasting the XML code in your pom.xml file between <dependencies> tags.
9. We also need to install JUnit, which is a unit testing framework of Java. We can do this by searching for "JUnit" and by repeating step 8. (Copying and pasting the XML code for the most recent stable version in your pom.xml file).
10. Right click on your src/test/java folder in the Package Explorer. Create New → Class. Name it "BrowserTest" and click "Finish."
11. Copy and paste the following code into the new
BrowserTest Class.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserTest {
public static void main(String[] args) {
String projectPath = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver",projectPath+"//drivers/chromedriver/chromedriver");
WebDriver driver = new ChromeDriver();
//go to google
driver.get("http://google.com");
//enter text in search text box
WebElement element = driver.findElement(By.<i>name</i>("q"));
element.sendKeys("Automation is awesome!");
//click on submit
element.submit();
//close browser
driver.close();
System.out.println("Test Completed Successfully.");
}
}
12. Run the file. It should run perfectly with no errors and the message "Test Completed Successfully."