Showing posts with label Webdriver. Show all posts
Showing posts with label Webdriver. Show all posts

Wednesday 6 November 2013

How to Identify popup window in Selenium Webdriver....




1. First Check whether its popup window or IFrame window by using IE Developer or Find bug tools.

2. If its popup window then use following code :

driver.findElement(By.xpath("")).click();

Set s=driver.getWindowHandles();

This method will help to handle of opened windows other than parent 



Iterator ite=s.iterator();

while(ite.hasNext())

{

String popupHandle=ite.next().toString();

if(!popupHandle.contains(mwh))

{

driver.switchTo().window(popupHandle);

/**/here you can perform operation in pop-up window**

//After finished your operation in pop-up just select the main window again

driver.switchTo().window(mwh);

}

}

This method will help to filter out of all opened windows


1) String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.

WebDriver popup = null;

Iterator windowIterator = browser.getWindowHandles();

while(windowIterator.hasNext()) { 

String windowHandle = windowIterator.next(); 

popup = browser.switchTo().window(windowHandle);

if (popup.getTitle().equals("Google") {

break;

}

}

2) Uncomment code for the same popup,

//action in popup "Google". 

popup.findElement(By.name("q")).sendKeys("Thoughtworks");

//action in popup "Google". 

popup.findElement(By.name("btnG")).submit();"


3) After the popup actions, switch the driver back to the parent window,


browser.close(); // close the popup.

browser.switchTo().window(parentWindowHandle); // Switch back to parent window.
If its IFrame then use following line.

String parentWindowHandle = driver.getWindowHandle(); 

driver.switchTo().Frame(driver.findelement(by.id("iframeid")));
// perform ur actions.
//revert back to parent control

driver.switchTo().window(parentWindowHandle);

Friday 26 July 2013

Upload Files in Selenium Webdriver

Please go through below code ......


package Sample;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class uploadfile {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.files.com/");
driver.manage().window().maximize();
WebElement uploadElement = driver.findElement(By.xpath(".//*[@id='uploadFields']/input"));
//WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
uploadElement.sendKeys("C:\\Users\\shivap\\Desktop\\24-1374658179-13.jpg");
driver.close();

}
}

Happy Testing 

Thursday 18 July 2013

Selenium grid Configuration With Screenshots


  Selenium grid Configuration With Screenshots


Overview:

Selenium Grid is a tool that dramatically speeds up functional testing of web-apps by leveraging your existing computing infrastructure. It allows you to easily run multiple tests in parallel, on multiple machines, in a heterogeneous environment. 
Based on the excellent Selenium web testing tool, Selenium Grid allows you to run multiple instances of Selenium Remote Control in parallel. Selenium Grid cuts down on the time required to run a Selenium test suite to a fraction of the time that a single instance of Selenium instance would take to run.

Selenium Grid Architecture




  Prerequisites:


1.      Need to download and install the latest Java JRE on system. JRE file can find at http://www.oracle.com/technetwork/java/index-jsp-141438.html

2.      Set the environment variables for java.

3.      Once environment setup is completed, open up a command prompt and run the command java -version. It should look something like the image below.



4.      Need to download Apache Ant and expand the archive to a folder on hard drive. Set the environment variables for Ant. Once setup is completed open up a terminal\
             Command prompt and type ant -version.

 It should look something like the image below



5.      Download latest version of selenium grid from

The folders structure should look like:         





    Running the sample test script:


Now that the environment and test data are set up, you can run two different scenarios of parallel testing:

1.      Test script run in different browsers on a server.

2.  Test script run in parallel on remote machines in different browsers.                                                                                                                                                                                                                    

1.Test script run in different browsers on a server


First we have to start our server. Fallow this commands using command prompt
Launching the Hub:
C:\>cd Program Files
C:\> Program Files\> cd java
C:\> Program Files\> java\> cd selenium-grid-1.0.8
C :\> Program Files\> java\> selenium-grid-1.0.8\> ant launch-hub

This will launch the Selenium Hub on 4444 port. It should look something like the image below.


Now we need to register 2 remote controls to Grid Hub. One is RC running for Firefox tests and the other for Google chrome tests.


               Open the new command prompt from the Grid directory and run the following
             Command.


C:\>cd Program Files
C:\> Program Files\> cd java
C:\> Program Files\> java\> cd selenium-grid-1.0.8
C :\> Program Files\> java\> selenium-grid-1.0.8\>ant -Dport=5556 -Denvironment=*firefox launch-remote-control  


 This will start Remote Control instance on 5556 and will be listening to Grid Hub.
Open another command prompt from the Grid directory and run the following command

C:\>cd Program Files
C:\> Program Files\> cd java
C:\> Program Files\> java\> cd selenium-grid-1.0.8
C :\> Program Files\> java\> selenium-grid-1.0.8\>ant -Dport=5556 -Denvironment=*firefox launch-remote-control 

This will start Remote Control instance on 5557 and will be listening to Grid Hub.

Open the browser and launching following URL http://localhost:4444/console

So far configured details can be seen by clicking above URL.


This is to make sure RCs registered to Grid Hub

 Selenium Grid Hub console













Available Remote Controls
     All the remote controls which we have launched above will be listed in Grid console window.

Active Remote Controls
              Remote Control to which scripts are currently running are called as active RC’S.
Executing Script on multiple browsers:

This script launches instances of Google chrome, Firefox browsers. It will open the www.gifts.com website and clicks on “birthday as occasion” link. It asserts whether the landing page displays the birthday gifts in all the two browsers as expected.


Test script:

     package gift Shop;
     import org.testng.annotations.Test;
     import org.testng.Assert;
     import org.testng.annotations.AfterClass;
     import org.testng.annotations.BeforeClass;
     import org.testng.annotations.Parameters;
     import com.thoughtworks.selenium.DefaultSelenium;
     import com.thoughtworks.selenium.Selenium;
    public class giftBox
    {
    Selenium selenium;
                //Declaration of variables used in this program
               String strTimeout="30000";
               String bdayTitle="Birthday Gifts - Birthday Gift Ideas from Gifts.com";
              int intsleepTime=15000;
               @BeforeClass

  //Reads the values of host, port, browser and url "www.gifts.com" from testng.xml

  @Parameters({ "selenium.host", "selenium.port", "selenium.browser",     "selenium.url" })



 public void beforeClass(String host, String port, String browser, String url)
               {
        selenium = new DefaultSelenium(host, Integer.parseInt(port),browser, url);
        selenium.start();
        selenium.open("");
        selenium.windowMaximize();
        selenium.windowFocus();
        selenium.deleteAllVisibleCookies();
        }
        @Test
        @Parameters({ "bdayLink" })
        public void testB(String bDayGift)
        {
        try
         {
         //Click on birthday as occasion on www.gifts.com website
         selenium.click(bDayGift);
         selenium.waitForPageToLoad(strTimeout);

         //Verify that the birthday gift items page is displayed

         Assert.assertEquals(bdayTitle, selenium.getTitle());
         Thread.sleep(intsleepTime);
         }
         catch (Exception e)
         {
          e.printStackTrace();
         }
         }
         @AfterClass
         public void afterClass()
         {
         selenium.close();
         selenium.stop();}}


.XML file :
As we are going to use multiple sets of data, XML file will help us to maintain data and it will be easy to understand and modify.

Create an xml file (ex: testng.xml) and specify the browser and port names with which we have configured our selenium remote control as in the below piece of code.

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">

            <test name="Firefox">
                        <parameter name="selenium.host" value="localhost"/>
                        <parameter name="selenium.port" value="5556"/>
                        <parameter name="selenium.browser" value="*firefox"/>
                        <parameter name="selenium.url" value="http://www.gifts.com/"/>
                        <parameter name="bdayLink" value="//li[@id='nav-oc']/div/ul[3]/li[3]/a/b"/>
                        <classes>
                                    <class name="giftShop.giftBox" />
                        </classes>
            </test>
           
<test name=" GoogleChrome ">
                        <parameter name="selenium.host" value="localhost"/>
                        <parameter name="selenium.port" value="5557"/>
                        <parameter name="selenium.browser" value="*googlechrome"/>
                        <parameter name="selenium.url" value="http://www.gifts.com/"/>
                        <parameter name="bdayLink" value="//li[@id='nav-oc']/div/ul[3]/li[3]/a/b"/>
                        <classes>
                                    <class name="giftShop.giftBox" />
                        </classes>
            </test>
</suite>



 2.  Test script run in parallel on remote machines in different browsers:

First we have to start our Grid Hub in one machine. Follow these commands using command prompt
Launching the Hub:

C:\>cd Program Files
C :\> Program Files\> cd java
C :\> Program Files\> java\> cd selenium-grid-1.0.8
C :\> Program Files\> java\> selenium-grid-1.0.8\> ant launch-hub

Start the nodes:

Now we need to register remote controls from first machine to hub.  now RC is running on Firefox browser.

             Open the command prompt from the Grid directory and run the following command.


C:\>cd Program Files
C :\> Program Files\> cd java
C :\> Program Files\> java\> cd selenium-grid-1.0.8
C :\> Program Files\> java\> selenium-grid-1.0.8\>ant -Dport=5555 –   Dhost=192.168.101.10 -DhubURL=http://192.168.101.10:4444 –Denvironment=*firefox launch-remote-control 


 This will start Remote Control instance on 5555 and will be listening to Grid hub which is launched in other system.
Now we need to register remote controls from second machine to hub.  Now RC is running on Google chrome.

C:\>cd Program Files
C :\> Program Files\> cd java
C :\> Program Files\> java\> cd selenium-grid-1.0.8
C :\> Program Files\> java\> selenium-grid-1.0.8\> ant -Dport=5557 –Dhost=<192.168.101.3> -DhubURL=http://localhost:4444        -Denvironment=*googlechrome launch-remote-control

This will start Remote Control instance on 5557 and will be listening to hub.

Open the browser and launch the following URL http://localhost:4444/console

So far configured details can be seen by clicking above URL.

This is to make sure RCs registered to hub.                                 

                                      Selenium Grid Hub console


Executing Sample Script in Multiple machines Using Grid:

This script launches Google chrome, Firefox browsers in multiple machines. It will open the www.gifts.com website and clicks on “birthday as occasion” link. It asserts whether the landing page displays the birthday gifts in all the two browsers in different machines as expected.


Test script:

package gift Shop;
import org.testng.annotations.Test;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class giftBox
{
Selenium selenium;
            //Declaration of variables used in this program
            String strTimeout="30000";
            String bdayTitle="Birthday Gifts - Birthday Gift Ideas from Gifts.com";
            int intsleepTime=15000;
             @BeforeClass

 //Reads the values of host, port, browser and url "www.gifts.com" from testng.xml

 @Parameters({ "selenium.host", "selenium.port", "selenium.browser",    "selenium.url" })



 public void beforeClass(String host, String port, String browser, String url)
               {
        selenium = new DefaultSelenium(host, Integer.parseInt(port),browser, url);
        selenium.start();
        selenium.open("");
        selenium.windowMaximize();
        selenium.windowFocus();
        selenium.deleteAllVisibleCookies();
        }
        @Test
        @Parameters({ "bdayLink" })
        public void testB(String bDayGift)
        {
        try
         {
         //Click on birthday as occasion on www.gifts.com website
         selenium.click(bDayGift);
         selenium.waitForPageToLoad(strTimeout);
         //Verify that the birthday gift items page is displayed
         Assert.assertEquals(bdayTitle, selenium.getTitle());
         Thread.sleep(intsleepTime);
         }
         catch (Exception e)
         {
          e.printStackTrace();
         }
         }
         @AfterClass
         public void afterClass()
         {
         selenium.close();
         selenium.stop();}}



.XML file:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">

            <test name="Opera">
                        <parameter name="selenium.host" value="192.168.101.10"/>
                        <parameter name="selenium.port" value="5557"/>
                        <parameter name="selenium.browser" value="*opera"/>
                        <parameter name="selenium.url" value="http://www.gifts.com/"/>
                        <parameter name="bdayLink" value="//li[@id='nav-oc']/div/ul[3]/li[3]/a/b"/>
                        <classes>
                                    <class name="giftShop.giftBox" />
                        </classes>
            </test>
           
<test name="GoogleChrome">
                        <parameter name="selenium.host" value="192.168.101.12"/>
                        <parameter name="selenium.port" value="5555"/>
                        <parameter name="selenium.browser" value="*googlechrome"/>
                        <parameter name="selenium.url" value="http://www.gifts.com/"/>
                        <parameter name="bdayLink" value="//li[@id='nav-oc']/div/ul[3]/li[3]/a/b"/>
                        <classes>
                                    <class name="giftShop.giftBox" />
                        </classes>
            </test>
</suite>