how to configure selenium webdriver to run in eclipse, programming with java

3
Dalton Valadares – Universidade Federal de Campina Grande Embedded Systems and Pervasive Computing Lab (Embedded) How to configure Selenium WebDriver to run with Eclipse programming in Java -Download the latest version of Selenium WebDriver Java client driver: http://selenium- release.storage.googleapis.com/2.40/selenium-java-2.40.0.zip (Currently, I am using the 2.39.0 version, but the 2.40.0 is already available). If you will use another programming language, you must download the corresponding driver for it (C#, Python, Ruby); -Extract the content of the selenium-java-2.40.0.zip in a selected folder; -Open Eclipse in your project or create a new one; -Add the Selenium JARs to your project: *Right click on your project's name; *'Properties' --> 'Java Build Path'; *Click on 'Add External JARs' button; *Select the two JARs in your Selenium folder (selenium-java-2.39.0-srcs.jar and selenium- java-2.39.0.jar) and click open; *Click on 'Add External JARs' button again; *Open the libs folder which is inside the Selenium folder; *Select all the JARs inside libs folder and click open; -Now, the Eclipse is configured to run Selenium WebDriver tests. Using Chrome and Internet Explorer to run tests with Selenium -If you will not use the Firefox browser to run your tests, for each browser, it is necessary to have its respective driver; -Download the latest version of each in the following links: *Chrome driver - http://chromedriver.storage.googleapis.com/index.html?path=2.9/ *Internet Explorer driver - http://selenium- release.storage.googleapis.com/index.html?path=2.40/ -Put them in your PATH (the path to the executable files in your Environment Variables); -Test with some code like the below one: public class TestRMS{ public static void main(String[] args) throws InterruptedException {

Upload: dalton-valadares

Post on 07-Jul-2015

2.670 views

Category:

Technology


2 download

DESCRIPTION

In this tutorial, it is presented how to configure the Selenium WebDriver to run in Eclipse IDE, programming with Java. Furthermore, it is presented how to make easier and faster the process of create tests and how to run tests remotely (in a remote machine).

TRANSCRIPT

Page 1: How to Configure Selenium WebDriver to Run in Eclipse, programming with Java

Dalton Valadares – Universidade Federal de Campina Grande

Embedded Systems and Pervasive Computing Lab (Embedded)

How to configure Selenium WebDriver to run with Eclipse programming

in Java

-Download the latest version of Selenium WebDriver Java client driver: http://selenium-

release.storage.googleapis.com/2.40/selenium-java-2.40.0.zip (Currently, I am using the 2.39.0

version, but the 2.40.0 is already available). If you will use another programming language, you

must download the corresponding driver for it (C#, Python, Ruby);

-Extract the content of the selenium-java-2.40.0.zip in a selected folder;

-Open Eclipse in your project or create a new one;

-Add the Selenium JARs to your project:

*Right click on your project's name;

*'Properties' --> 'Java Build Path';

*Click on 'Add External JARs' button;

*Select the two JARs in your Selenium folder (selenium-java-2.39.0-srcs.jar and selenium-

java-2.39.0.jar) and click open;

*Click on 'Add External JARs' button again;

*Open the libs folder which is inside the Selenium folder;

*Select all the JARs inside libs folder and click open;

-Now, the Eclipse is configured to run Selenium WebDriver tests.

Using Chrome and Internet Explorer to run tests with Selenium

-If you will not use the Firefox browser to run your tests, for each browser, it is necessary to

have its respective driver;

-Download the latest version of each in the following links:

*Chrome driver - http://chromedriver.storage.googleapis.com/index.html?path=2.9/

*Internet Explorer driver - http://selenium-

release.storage.googleapis.com/index.html?path=2.40/

-Put them in your PATH (the path to the executable files in your Environment Variables);

-Test with some code like the below one:

public class TestRMS{

public static void main(String[] args) throws InterruptedException {

Page 2: How to Configure Selenium WebDriver to Run in Eclipse, programming with Java

System.setProperty("webdriver.chrome.driver",

"C:\\Users\\dalton\\Documents\\RMS\\chromedriver.exe");

System.setProperty("webdriver.ie.driver",

"C:\\Users\\dalton\\Documents\\RMS\\IEDriverServer.exe");

WebDriver chromeDriver = new ChromeDriver();

executeTest(chromeDriver);

chromedriver.close();

DesiredCapabilities capabilities =

DesiredCapabilities.internetExplorer();

capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKIN

ESS_BY_IGNORING_SECURITY_DOMAINS, true);

WebDriver ieDriver = new InternetExplorerDriver(capabilities);

executeTest(ieDriver);

ieDriver.close();

}

private static void executeTest(WebDriver driver) throws

InterruptedException {

driver.get("http://www.google.com");

driver.findElement(By.id("gbqfq")).clear();

driver.findElement(By.id("gbqfq")).sendKeys("Selenium

Webdriver");

System.out.println(driver.getCurrentUrl() + " " +

driver.getTitle());

driver.findElement(By.cssSelector("em")).click();

System.out.println(driver.getCurrentUrl() + " " +

driver.getTitle());

}

}

-If you have problem executing the test with Internet Explorer driver, maybe you should try to

change the Protected Zone in the browser (Protected Mode must be set to the same value,

enabled or disabled, for all zones). Option found in the Advanced tab of the Internet Options

dialog.

Accelerating your tests creation

-For make the process of test creation faster, install the Firefox Selenium Plugin (Selenium

IDE): http://release.seleniumhq.org/selenium-ide/2.5.0/selenium-ide-2.5.0.xpi (let the Firefox

install this plugin);

-With the Selenium IDE installed on Firefox, open it, clicking in Tools --> Selenium IDE;

Page 3: How to Configure Selenium WebDriver to Run in Eclipse, programming with Java

-The Selenium IDE will open and you can record some commands with it, clicking in the red ball

in right upper corner or clicking the right option in Menu Actions;

-After you record some actions of your test, you must to import your script to run with other

browsers. To do this, click on Menu File --> Export Test As... --> Java / JUnit4 / WebDriver (here

you can choose any other type of file, with a different programming language, for example);

-This way, you can edit your test in Eclipse, for example, and run your tests with other

browsers.

Running tests in a remote browser (remote machine)

-To run tests in a remote browser, you have to download the Selenium Grid in the remote

machine: http://selenium-release.storage.googleapis.com/2.40/selenium-server-standalone-

2.40.0.jar (this link is for 2.40.0 version)

-In a command prompt/terminar, run the following command: java -jar ./selenium-server-

standalone-2.40.0.jar -Dwebdriver.chrome.driver='/your/path/to/chromedriver.exe' (if you

will use other browser, like IE, you must to change this parameter/path at the command);

-In your code, to run the test(s) remotely, put code like the below:

DesiredCapabilities capability = DesiredCapabilities.chrome(); //If

you want to change the browser, change the capability here...

capability.setPlatform(Platform.WINDOWS);

//setPlatform(Platform.LINUX);

capability.setVersion("10");

try {

driver = new RemoteWebDriver(new

URL("http://your.remote.IP.Address:4444/wd/hub"), capability); //put your

remote IP here

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

-Run your tests.

Now, you can make unit tests with Selenium, in a fast way and run them into your machine or

remotely.

I hope this tutorial might be helpful.