Showing posts with label integration. Show all posts
Showing posts with label integration. Show all posts

Friday, 15 July 2016

Selenium Remote WebDriver + Sauce Labs

Sauce Labs is a Monster Grid to run our tests remotely.

The Sauce Labs cloud-based testing platform helps you run your SeleniumWebdriver tests on the cloud, test web and mobile apps immediately on 350+ browsers and platforms, and easily debug your code with the help of videos and screenshots.

Lest quickly setup the things.


Step 1: Create a sauce labs account: https://saucelabs.com/home

Choose Free Trail, fill the info and then click on Create Account button.



Then verify your account by clicking link in the Email.

Now, you can see Dashboard screen.



Observe, Bottom left corner where you can see your account Firstname and Last name.

Click on it,



We need UserName and AccessKey to connect to sauce labs from our selenium code.



Step 2: Write Remote WebDriver code:

Here is the sample code.

package com.sauceLabs.automation;

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class Test {

       public static final String USERNAME = "kXXXXXX";
       public static final String AUTOMATE_KEY = "d87XXXX-XXX-XXX-XXX-XXX";
       public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@ondemand.saucelabs.com:80/wd/hub";

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

              DesiredCapabilities caps = new DesiredCapabilities();
              caps.setCapability("browser", "IE");
              caps.setCapability("browser_version", "7.0");
              caps.setCapability("os", "Windows");
              caps.setCapability("os_version", "XP");
              caps.setCapability("browserstack.debug", "true");

              WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
              driver.get("http://www.google.com");
              WebElement element = driver.findElement(By.name("q"));

              element.sendKeys("BrowserStack");
              element.submit();

              System.out.println(driver.getTitle());
              driver.quit();

       }

}


Just run it, That’s all.







Thursday, 4 December 2014

Integrating AutoIt with Selenium


What is AutoIt:

       AutoIt is freeware scripting language for automating the windows GUI.

      
Why is AutoIt:
       When you start working with web-application so many time you will get window based pop-up like- file upload, download pop up, window authentication etc. In this case Selenium fails and will not be able to handle desktop elements to avoid this we will use AutiIt script that will handle desktop or windows elements and will combine AutoIt script with our Selenium code.

Setup / Configuration:
Download and Install AutoIt software and Editor here



Install the software.

IMAGE 2:

Search for “sciTE Script Editor” or “SciTE.exe” in RUN dialog.


Problem statement:

Automate file upload feature in http://www.dropzonejs.com/

We know how to write selenium code until we click upload button and here is the code.

public static void main(String[] args) throws InterruptedException, IOException {
             
              String baseUrl = "http://www.dropzonejs.com/";
              driver = new FirefoxDriver();
              driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
              driver.get(baseUrl);
              driver.manage().window().maximize();
              Thread.sleep(6000);             
              driver.findElement(By.id("demo-upload")).click();
              Thread.sleep(3000);
       }

Now, we need to select a file from windows explorer and Selenium cannot do it for obvious reasons.

But, we can achieve this using AutoIt.

Here is the AutoIt script:

; Wait 10 seconds for the Upload window to appear
  WinWait("[CLASS:#32770]","",10)

; Set input focus to the edit control of Upload window using the handle returned by WinWait
  ControlFocus("File Upload","","Edit1")
  Sleep(2000)

; Set the File name text on the Edit field
  ControlSetText("File Upload","","Edit1","C:\Users\testUser\Downloads\A.jpg")
  Sleep(2000)

; Click on the Open button
  ControlClick("File Upload", "","Button1");


Explanation:
WinWait pauses execution of the script until the requested window existed
Now, we need to understand how to locate the file upload windows explorer.
Open      Au3Info.exe (Window Info tool), select Finder tool and point to file upload windows explorer to locate it.

We have two parts in ‘Window Info tool’, one is ‘Basic window Info’ and other is ‘Basic Control Info’.

As we are dealing with windows explorer wait, lets concentrate on ‘Basic window info’, notice about CLASS id to locate the window as ([CLASS:#32770])

Similarly we can locate each and every windows explorer element.

Few Methods to remember:

ControlFocus Sets input focus to a given control on a window.
ControlSetText Sets text of a control.
ControlClick  Sends mouse click command to a given control.

SAVE the file:

Save AutoIt script file as with ‘.au3’ extension.  Ex: one.au3


Compiling au3 script file:

Right click on one.au3 file and select compile script option.
A new file named one.exe will be generated.

Integrating AutoIt

with Selenium:

public static void main(String[] args) throws InterruptedException, IOException {
             
              String baseUrl = "http://www.dropzonejs.com/";

              driver = new FirefoxDriver();
              driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
              driver.get(baseUrl);
              driver.manage().window().maximize();
              Thread.sleep(6000);
             
              driver.findElement(By.id("demo-upload")).click();
              Thread.sleep(3000);
             
              Runtime.getRuntime().exec("E:/one.exe");       

       }     


Just run the script, that’s all.


NOTE: We do not need to add any jars to the project for this to run.