...
Prerequisites (Downloads and Installing)
It is recomended recommended to create and run your tests on a linux operating system or in a VirtualBox linux environment. in order to be able to run Java, Selenium WebDriver Tests the following will need to be installed on your computer:
- Java 8 JDK
- Can be found:
- Check to see if java is installed already and what version you are currently using
java -version
If not installed or using and older verision use the following instructions/commands
sudo apt-get update
sudo apt-get upgrade
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jdk
sudo apt-get update
close and reopen terminal
sudo update-alternatives --config java
- Ensure that there is a file named 'java-8-openjdk-amd64
ls
/usr/lib/jvm/
- Ensure that there is a file named 'java-8-openjdk-amd64
- Eclipse Mars 1
- Can be found:
- Selbenium WebDriver Server .jar files
- Can be found: http://www.seleniumhq.org/download/
- Google Chrome Web Browser
- Selenium ChromeDriver
- Firefox Web Browser
- Can be found: https://www.mozilla.org/en-US/firefox/
- Selenium FirefoxDriver (i.e: geckoDriver )
Can be found: https://github.com/mozilla/geckodriver/releases
Recommended: Install the Firebug add-on for Firefox as it can help to find Ids, css, and xpaths that will need to include in your code.
...
In order to begin writing Selenium/Selenese Code, you will need to configure your Eclipse Properties to allow running of Selenium Functions and methods. The following are the steps that need to be taken when creating new.
Step 1:
- Open Eclipse Mars 1 and choose a workspace.
- Click on File, New, Java Project and name you’re Java Project
- Under JRE select JavaSE-1.8 and click Finish.
You will see that under the created project, a new src folder and the JRE System Library folder have been created.
Step 2:
Go the Selenium website and download Selenium Standalone Server and the respective Selenium Client & WebDriver Language Binding. Extract the jar files in a folder. They Can be found at: http://www.seleniumhq.org/download/
Step 3:
To create Selenium WebDriver Test Cases you will need to configure the Selenium WebDriver Functionality by adding jar files to the project.
- Right click on project folder, go to properties, Java Build Path and select Libraries.
- The JRE System Library will already be included.
- Click Add External JARs.. and add the selenium-server-standalone-3.4.0.jar, client-combined-3.4.0-nodeps.jar, and all the jar files in the “lib” folder.
Once the jar files have been added the user can view them in the Referenced Libraries folder under Package Explorer.
Example Code
Setting System Environment Variables
...
System.setProperty("webdriver.chrome.driver", "/home/<username>/file/path/to/chromedriver");
There are some functional issues with opening a firefox browser, so at this time it is recommended to use Google Chrome.
Maximizing Browser Window Size
So Tests when conducted tend to ste=op or fail if the browser window has not been maximized. so the following line of code can be used to maximize the browser window:
driver.manage().window().maximize();
Visiting a Web Page
driver.get("http://beta.opendmc.org");
...
driver.findElement(By.if("idOfElement")).sendKeys("Text to add to Text field");
driver.findElement(By.cssSelector("cssToElementLocation")).sendKeys("Text to add to Text field");
driver.findElement(By.xpath("actualXpathToElement")).sendKeys("Text to add to Text field");
Using Keyboard Commands
...
and Uploading a Document
The following function was created to allow for the browser to automatically be able to take a file name provided as a string to then be automatically found and uploaded to the website.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;
public static void fileLocationUpload(String documentLocation)
import java.awt.Robot;
{
try
{
//System.out.println("About to press down.");
Robot robot1 = new Robot();
robot1.delay(1000);
robot1.keyPress(KeyEvent.VK_DOWN);
robot1.keyRelease(KeyEvent.VK_DOWN);
robot1.delay(1000);
//System.out.println("Pressed down already.");
}
catch (Exception e)
{
e.printStackTrace();
}
//System.out.println("Typing out file location.");
for(int i = 0; i < documentLocation.length(); i++)
{
try
{
Robot robot = new Robot();
//robot.delay(1000);
//char letter = '/';
char letter = documentLocation.charAt(i);
if (letter != '/' && letter != '.' && letter != ' ' && letter != '-' && letter != '_')
{
//System.out.println("I'm inside the IF! ...Somehow.");
boolean upperCase = Character.isUpperCase(letter);
char upperLetter = Character.toUpperCase(letter);
String variableName = "VK_" + upperLetter;
Class clazz = KeyEvent.class;
Field field = clazz.getField( variableName );
int keyCode = field.getInt(null);
if (upperCase) robot.keyPress( KeyEvent.VK_SHIFT );
robot.keyPress( keyCode );
robot.keyRelease( keyCode );
if (upperCase) robot.keyRelease( KeyEvent.VK_SHIFT );
}
if (letter == '/')
{
robot.keyPress( KeyEvent.VK_SLASH );
robot.keyRelease( KeyEvent.VK_SLASH );
}
if (letter == '.')
{
robot.keyPress( KeyEvent.VK_PERIOD );
robot.keyRelease( KeyEvent.VK_PERIOD );
}
// if (letter == '\')
// {
// robot.keyRelease( KeyEvent.VK_QUOTE );
// }
if (letter == ' ')
{
robot.keyPress( KeyEvent.VK_SPACE );
robot.keyRelease( KeyEvent.VK_SPACE );
}
if (letter == '-')
{
robot.keyPress( KeyEvent.VK_MINUS );
robot.keyRelease( KeyEvent.VK_MINUS );
}
if (letter == '_')
{
robot.keyPress( KeyEvent.VK_UNDERSCORE );
robot.keyRelease( KeyEvent.VK_UNDERSCORE );
}
}
catch(Exception e)
{
System.out.println(e);
}
}
try
{
Robot robot2 = new Robot();
robot2.keyPress(KeyEvent.VK_ENTER);
robot2.keyRelease(KeyEvent.VK_ENTER);
robot2.delay(1000);
robot2.keyPress(KeyEvent.VK_ENTER);
robot2.keyRelease(KeyEvent.VK_ENTER);
robot2.delay(1000);
}
catch(Exception e)
{
System.out.println(e);
}
} |
Wait for Element to Load
WebDriverWait wait = new WebDriverWait(driver, 10);
...
WebElement workspaceNameCreated = driver.findElement(By.cssSelector("cssToElementLocation"));
String workspaceNameCreatedStr = "Workspace name Not Found";
workspaceNameCreatedStr = workspaceNameCreated.getText();
System.out.println(workspaceNameCreatedStr);
...