Selenium First Steps

Hello guys !!

I have a friend that asked to me somethings to start in Selenium’s world. For this reason I will write today about first steps to automate in Selenium.

In this example, we will open in the Chrome browser. Install the Chrome in your Operational System and after that, you have to download the driver too.

The link to download the driver is: http://chromedriver.storage.googleapis.com/index.html

Now, you have to download the WebDriver’s lib. You can download in this link: http://docs.seleniumhq.org/download/

Import the lib in your Java project. This is how the structure will looks like. Pay attention on the folder of Libraries in this picture below.

structure

This is the package and the libs imported to use in our automation:

libs_import

This is the code to use the chrome’s driver. You have to put this, because the Chrome won’t open without the driver and this line. If you are using Windows, you have to download the .exe file. Each OS that you use, you have a specific file to download. After write this line, you will instantiate the ChromeDriver and write the command below, to open a site in Chrome.

comands-initial

You can find an element of different ways. You can use the id, the classname, tagname, etc… Xpath is the last option that you have to use, because there is a lot of problems when you have to use it in mutiple browsers. So, you can look all the possibilities in the picture below:

how-find-element-and-use

 

 

This is the correct way to wait some element to load on your page. You have to put this after load a new page, or click on a new function. It is better put this expected conditions to wait a element than put a time to wait, because unfortunately we depend a lot of environment and browser.

how-wait-until

You can use these commands to click and write something in specific field, respectively. You can instantiate the element and you will use only one time to search it. After that, you can use the instance to click, send words and others actions in the element without looking it again.

commands

This is the command to close the browse, to close the driver, respectively, and finish your automation. After this, you can’t use the driver anymore, unless you instantiate again another WebDriver.

how-close-browser

This is the final code ! Maybe your code will looks like with mine. Afterward, just put to run and see the automation !

code-complete

Bye guys !!

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: