SELENIUM WEB DRIVER WITH JAVA

Selenium is an open-source tool that is used for test automation. Selenium-IDE is an Firefox plug-in to develop Selenium test cases. But in this post I’m not going to talk about the Selenium-IDE. Here we are talking about a tool in Selenium, which is known as Selenium WebDriver introduced for UI Automation. From this link you can download the WebDriver, actually a set of jar files.

http://www.seleniumhq.org/download/

I’m using the WebDriver with JAVA programming language. Next we need to create a java project. I’m using the IntelijIDEA IDE for this.

  • First open the IDE.

1.png

  • Next go to File –> New –> Project.

2.png

  • Next choose JAVA from the left column.

3

  • Click next, On the following window, give a name for the project, a location to save the project and then click next.

4

5

  • Project has been created successfully.

6

7

  • Then right click on the src folder and goto add –> JAVA class.

8

9

  • Then we need to add the downloaded Selenium WebDriver Libraries to our project. Go to File –> Project Structure.

11.png

  • Click Libraries form the left panel. Then Click on the + sign.

12

13

  • From the drop down select JAVA.

14.png

  • Then we can add the relevant JAR files from the stored location. You need to add all the libraries in the lib directory, selenium-java-2.50.1.jar and selenium-java-2.50.1-srcs.jar as well.

15

16

17.png

  • Libraries has been added successfully. Now we are ready to go.

18

  • Now type the following code inside your class.

First of all we have to import java.util.concurrent.TimeUnit, org.openqa.selenium.* and org.openqa.selenium.firefox.FirefoxDriver to our project.

Then we can create a new FirefoxDriver object.

Next we have given a waiting time for the code.

Next navigating to a relevant we page.

Form the next line it will put the opened browser window to full screen.

Next we are closing the browser.

19.png

  • Now you can run the project by clicking the Green Arrow icon on the right top corner.

20.png

  • Then the following browser window will open and after sometime it will close automatically.

21.png

Now we have successfully completed a project and add WebDriver to it as well. But that’s not all. There are some commands which can be useful in doing UI automation. We have to deal with text boxes, check boxes, radio buttons, buttons and etc. Let’s see them with few examples.

  • Now I want to find the ID‘s of few text boxes in the above page. I’m using FireBug and FirePath firefox extensions to find these.

23

24

25

  • Now add the following code to your existing code.

findElement() is used to a find a specific element from the page.

By.id() is used to point out an element using it’s ID.

sendKeys() is used to send data to the web page.

By.xpath(), here the xpath is found using the FirePath.

click() is used to click the button on the page.

getAttribute() is used to get a attribute of the page. Here we are getting the “value” of “cpar1”. That’s what we entered.

26.png

  • Now run the project and see the magic!

First it will open the browser.

27.png

Then it will enter the amount to the first text box.

30

Then enter the next amount to the next text box.

31.png

Next it will click the “Calculate” button t produce the output.

32.png

  • After the window automatically closed down, you can see the output terminal of the IDE.

33.png

That’s how to work with Selenium WebDriver. The post has become very long. So I’ll continue the rest of the commands of WebDriver from another post. Keep in touch. Thank You!

Advertisements

10 thoughts on “SELENIUM WEB DRIVER WITH JAVA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s