How to integrate Guice + Cucumber + WebDriver

Hello guys,

Today I am going to post about something that I have been studying. So, in this project you can have an idea of how to implement Dependency Injection using Guice, a Google framework which helps you to reduce the need of new in your Java code. I found only projects that have only the Guice + Cucumber integration or only the Guice + Page Objects, so I think this one might be a good example of how to integrate everything.

Also, the project contains Page Objects and a Driver Factory, so you will be able to include other browsers and implement each one’s singularity when creating the browser, and also you still have the good practices of the POM.

I’ve added comments along the code, but feel free to ask anything or even suggest improvements.

https://github.com/rafaelaazevedo/dog-lightning

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 !!

Open saved profiles in Firefox – Selenium (WebDriver)

Hello, I will put the code that you need for open some saved profile in Firefox with Selenium.

ProfilesIni profilesIni = new ProfilesIni();
FirefoxProfile profile = profilesIni.getProfile(“default“); //This is the name of profile, you can find this in the folder of firefox profiles.
WebDriver driver = new FirefoxDriver(profile);
 
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); // Only necessary if you have invalid certificates in the page
                
profile.setPreference(“network.proxy.type”, 0); // open without proxy
profile.setAcceptUntrustedCertificates(true); // Only necessary if you have invalid certificates in the page
profile.setAssumeUntrustedCertificateIssuer(false); // Only necessary if you have invalid certificates in the page
 
I use this way for open a saved profile that contains a personal certificate. I needed open 
a page with this certificate installed.
 
Bye bye 🙂

Context click with actions (WebDriver)

Hello !!

 

Do you need simulate a context click (right-click) in some element ?

With the lib actions is simple. First you need import the actions of java and after

 

Actions actions = new Actions(driver);     //Instance the Actions

WebElement Element = driver.findElement(By.id(“ID“)); // find the element and put is in a variable

actions.ContextClick(Element).Perform(); // position the mouse over the element and context click it.

 

Just this, in some cases you have put a speed more lower or a wait element os something like that to work well.

 

Bye bye !!

 

How we can find a line in some table with Webdriver (Selenium) ?

Hi guys, I’m very occupied in the last months, but I will post a text about catch the lines and values in some table with Webdriver (selenium) in language java. Yes, I am changing a little the subjects, this time I will help the testers, who do automation of sites.

It’s easy if you use XPath.

This table:

Apples 44%
Bananas 23%
Oranges 13%
Other 10%

/html/body/div[1]/div/div[4]/div[2]/div[2]/table/tbody/tr[1]

You catched the first line.

If you want catch the second line, just change the number:

/html/body/div[1]/div/div[4]/div[2]/div[2]/table/tbody/tr[2]

It’s more easy than you expected, not ?

if you want catch all the text of all lines, just write the code:

WebElement listatable = driver.findElement(By.className(“reference”));

for (int i = 0; i < listatable.getLength(); i++) {

WebElement line = driver.findElement(By.xpath(“/html/body/div[1]/div/div[4]/div[2]/div[2]/table/tbody/tr[” + i + “]”));

line.getText();

}

Fine, now you can catch the text of each line in this table !

I hope has helped the initial testers in something.

Bye bye !