Showing posts with label selenium webdriver. Show all posts
Showing posts with label selenium webdriver. Show all posts

Thursday 2 January 2014

Select methods in selenium Webdriver...

Select Methods
The following are the most common methods used on drop-down elements.

selectByVisibleText() :
deselectByVisibleText():

Selects/deselects the option that displays the text matching the parameter.
Parameter: The exactly displayed text of a particular option

selectByValue()
deselectByValue()

Selects/deselects the option whose “value” attribute matches the specified parameter.
Parameter: value of the “value” attribute remember that not all drop-down options have the same text and “value”, like in the example below.

selectByIndex()
deselectByValue()


Selects/deselects the option at the given index.
Parameter: the index of the option to be selected


isMultiple()

Returns TRUE if the drop-down element allows multiple selections at a time; FALSE if otherwise.
Needs parameters needed

deselectAll ():


Clears all selected entries. This is only valid when the drop-down element supports multiple selections.

No parameters needed

Navigate commands in Selenium Webdriver...


Navigate commands:
 These commands allow you to refresh go-into and switch back and forth between different webpages.

navigate ().to() :        It automatically opens a new browser window and fetches the
                                 page that  you specify inside its parentheses.

                                It does exactly the same thing as the get() method.

navigate().refresh() : It refreshes the current page


navigate().back():       Takes you back by one page on the browser’s history.

navigate().forward():  Takes you forward by one page on the browser’s history.

Thursday 1 August 2013

Grid with SeleniumWebdriver


import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URL;

import org.junit.AfterClass;
 import org.openqa.selenium.*;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.ie.InternetExplorerDriver;
 import org.openqa.selenium.remote.DesiredCapabilities;
 import org.openqa.selenium.remote.RemoteWebDriver;
 import org.testng.annotations.*;

public class GridWithWebdriver {

 public WebDriver driver;

 @Parameters({"browser"})
 @BeforeClass
 public void setup(String browser) throws MalformedURLException {

 DesiredCapabilities capability=null;
 //setting Chrome driver location
 System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\lib\\chromedriver.exe");
if(browser.equalsIgnoreCase("firefox")){
 System.out.println("firefox");
 capability= DesiredCapabilities.firefox();
 capability.setBrowserName("firefox");
capability.setPlatform(org.openqa.selenium.Platform.ANY);
 //capability.setVersion("");
 }

 if(browser.equalsIgnoreCase("chrome")){
 System.out.println("chrome");
 capability= DesiredCapabilities.chrome();
 capability.setBrowserName("chrome");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
 //capability.setVersion("");
 }

 driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
 driver.manage().window().maximize();
 driver.navigate().to("http://google.com");

 }

 @Test
 public void test_first() throws InterruptedException{
 Thread.sleep(3000);
 WebElement search_editbox = driver.findElement(By.name("q"));
 WebElement search_button = driver.findElement(By.name("btnG"));
 search_editbox.clear();
 search_editbox.sendKeys("first");
 search_button.click();
 driver.navigate().to("http://yahoo.com");
 String MYURL=driver.getCurrentUrl();
 System.out.println(MYURL);
 Thread.sleep(3000);
 }

 @Test
 public void test_second() throws InterruptedException{
 driver.navigate().to("http://CNN.com");
 String MYA=driver.getCurrentUrl();
 System.out.println(MYA);
 Thread.sleep(3000);
 driver.navigate().to("http://google.com");
 WebElement search_editbox=driver.findElement(By.name("q"));
 WebElement search_button=driver.findElement(By.name("btnG"));
 search_editbox.clear();
 search_editbox.sendKeys("second");
 search_button.click();
 }

 @AfterClass
 public void tearDown(){
 driver.close();
 }
 }
 //-Then started the selenium grid as per the below.

java -jar selenium-server-standalone-2.32.0.jar -role hub
 java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=WINDOWS
 java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=chrome,platform=WINDOWS -port 5556

//My testng.XML file as per the below.

<suite name="Same TestCases on Different Browser" verbose="3" parallel="tests" thread-count="2">
<test name="Run on Mozilla Firefox">
 <parameter name="browser" value="firefox"/>
 <classes>
 <class name="GridWithWebdriver"/>
 </classes>
 <test name="Run on goolge Chrome">
 <parameter name="browser" value="chrome"/>
 <classes>
 <class name="GridWithWebdriver"/>
 </classes>
 </test>
</test>
</suite>


When i ran the XML file as testng suite below error occurred.Hope any one may able to help.



Friday 26 July 2013

Sauce Labs With Selenium Webdriver



Overview

Ø  Sauce on Demand is a cloud-based service that allows you to run 
automated cross- browser functional tests at high speeds in parallel, 
so you   don’t need to maintain testing infrastructure.

Ø  Easy-to-use tools let you build both manual and automated tests 
that produce media-rich results, including screen shots and videos 
of bugs that can be added to any bug tracking system.

Ø  Sauce Support 65+browser and OS combination.

Ø  Operating Systems like Windows, Linux, Mac,

Ø  Browsers like Internet Explorer, Chrome, Firefox, Safari, Opera,
 I Phone, IPod, Android.

Ø  All latest browsers versions are available.

Ø  It has commercial support for selenium.

SAUCE CONNECT

Ø  Sauce Connect securely proxies browser traffic between Sauce 
Labscloud-based 
VMs and your local servers. Connect uses ports 443 and 80for communication
 with Sauces cloud.

Ø  Sauce Connect helps to do HTML Layout Testing.

Ø  This can be used by both Front End designers and Testers for performing manual compatibility testing.

Configure Sauce Connect

Ø   First we need to register in https://saucelabs.com/ website.

Ø  Download Sauce Connect.jar file from the Sauce labs site.

Ø  Run the Jar as below 
java -jar Sauce-Connect.jar YOUR-SAUCE-USERNAME(sivaprasadk) 
YOUR- SAUCE-API-KEY(d75aaba0-ed9c-4ec8-aa35-17c5ed17b402)

Ø  Login to the Sauce Labs web site using valid user credentials.( Here I have given my        credentials mentioned in previous point)

Ø  Select “New Interactive Session”
Ø  Browse the url`s (we can use the localhost url`s as well).

Screenshot of configured sauce Connect:

 Example Script for Sauce:

package sample;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;

public class Test2 {
private WebDriver driver;
@Test

public void setUp() throws Exception {
  DesiredCapabilities capabilities = DesiredCapabilities.firefox();
  capabilities.setCapability("version", "5");
  capabilities.setCapability("platform", Platform.windows);

  this.driver = new RemoteWebDriver(new URL
("http://sivaprasadk:d75aaba0-ed9c-4ec8-aa35-17c5ed17b402@ondemand.saucelabs.com:80/wd/hub"),
                capabilities);
    }

   
@Test
    public void Login_Paytm() throws Exception {
   
driver.get("https://www.paytm.com/");
driver.findElement(By.cssSelector("span")).click();
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("9880070959");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("Proceed")).click();
driver.findElement(By.id("opns")).click();
driver.findElement(By.id("loggedout")).click();
driver.quit();
}}

 Integration of Web driver and Sauce Services Sauce support Web 
driver scripts and can compatible with multiple programming languages 
like Java, Python, NodeJs, Perl. Here,

We are using java language for Integration and Functional Test Automation 
    (Selenium2 with java bindings).

Steps to Follow:

Ø    A Java File is written, which will take browsers Name, OS and browser 
    version as run time arguments.

Ø   The Script reads the inputs and invokes the Sauce browser with the given
    OS and browser combination in Sauce Cloud.

Ø   On invoking the Selenium functional script the tests will get executed in
    the Sauce Cloud.

Ø   Here we used HtmlTestRunner(unit test module for python) for maintaining
    test suite.

Ø   The test results can be captured from Sauce or from Html Test Report 
    (which will be generated using HtmlTestRunner).

 Dashboard – Sauce

    Like below we can find the results after completing test scripts.



Features of Sauce Cloud:

·       Developer Tools in Every Browser
·       Selenium RC & WebDriver Compatible
·       Video Recordings of Every Test
·       Test Local and Firewalled Servers
·       Any Programming Language
·       Browsers and Mobile Emulators

Advantages:

·       Easy to Integrate with Continuous Integration and Selenium2 test scripts.
·       Able to run the Parallel functional tests, which helps to optimize the 
    test execution Time.
·       Supports multiple browsers and OS combination including mobile browsers.
·       By using this approach, we can perform Functional, Layout, Compatibility 
    testing.
·       Sauce, in built support of Video recording and Screenshot capturing is very
    helpful and good.
·       Availability of updated Browsers and Versions.
·       Secure Tunnel system.

Dis Advantages:

·       Issues with Corporate Proxy environment.
·       As this tool works over cloud script runs very slowly.

References:

    http://saucelabs.com/features 

Tuesday 2 July 2013

TestNG+Ant+eclipse XSLT Report Generation

XSL stands for Extensible Style sheet Language, and is a style sheet 
Language for XML documents. XSLT stands for XSL Transformations.
 In this tutorial you will learn how to use XSLT to transform XML 
Documents into other formats, like XHTML. XSLT which gives good graphical generated reports.

Steps for generating reports:
2. Configure environment variable for ant 
      User variable
         variable name: ANT_HOME
         variable value: C:\Program Files\Java\apache-ant-1.9.1
     System variable
         variable name:  Path
         variable value:
 C:\Program Files\Java\apache-ant-1.9.1\bin
           Check ANT version
            cmd > ant -version



1. Go to project directory 
2.Go to src folder
3. Create a folder name as "xslt"
4. Download https://github.com/prashanth-sams/testng-xslt-1.1.2
5.Unzip it
6.Go to fallowing directory "testng-xslt-1.1.1\testng-xslt-1.1.1\src\main\resources"

7. Copy the "testng-results.xsl" and keep in "xslt

8. Now copy the saxon.jar file from fallowing path "testng-xslt-1.1.1\testng-xslt-1.1.1\lib".

9. place it your project folder.

10. Modify your build.xml of ant and add the following target to it.

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [
]>

<project name="TestNG" default="usage" basedir=".">  

<!-- ========== Initialize Properties =================================== -->
    <property environment="env"/>
    <property name="ws.home" value="${basedir}"/>
          <property name="ws.jars" value="F:\1"/>
    <property name="test.dest" value="${ws.home}/build"/>
    <property name="test.src" value="${ws.home}/src"/>
          <property name="ng.result" value="test-output"/>
    
    <!--target name="start-selenium-server">
        <java jar="${ws.home}/lib/selenium-server.jar"/>
    </target-->

    <target name="setClassPath" unless="test.classpath">
        <path id="classpath_jars">
            <fileset dir="${ws.jars}" includes="*.jar"/>
        </path>
        <pathconvert pathsep=":" 
            property="test.classpath" 
            refid="classpath_jars"/>
    </target>

    <target name="init" depends="setClassPath">
        <tstamp>
            <format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
        </tstamp>
        <condition property="ANT" 
            value="${env.ANT_HOME}/bin/ant.bat" 
            else="${env.ANT_HOME}/bin/ant">
            <os family="windows" />
        </condition>
        <taskdef name="testng" classpath="${test.classpath}"
               classname="org.testng.TestNGAntTask" />
    
    </target>
  
    <!-- all -->
    <target name="all">
    </target>

    <!-- clean -->
    <target name="clean">
        <delete dir="${test.dest}"/>
    </target>

    <!-- compile -->
    <target name="compile" depends="init, clean" > 
                   <delete includeemptydirs="true" quiet="true">
            <fileset dir="${test.dest}" includes="**/*"/>
                   </delete>
        <echo message="making directory..."/>
                   <mkdir dir="${test.dest}"/>
        <echo message="classpath------: ${test.classpath}"/>
        <echo message="compiling..."/>
        <javac 
            debug="true" 
            destdir="${test.dest}" 
            srcdir="${test.src}" 
            target="1.5" 
            classpath="${test.classpath}"
        >
        </javac>
      </target>

    <!-- build -->
    <target name="build" depends="init">
    </target>

    <!-- run -->
    <target name="run" depends="compile">
<testng classpath="${test.classpath}:${test.dest}" suitename="sivaprasad"> 
            <xmlfileset dir="${ws.home}" includes="testng.xml"/>
        </testng>
        <!--
        <testng classpath="${test.classpath}:${test.dest}" groups="fast">
            <classfileset dir="${test.dest}" includes="example1/*.class"/>
        </testng>
        -->
    </target>

    <target name="usage">
        <echo>
            ant run will execute the test
        </echo>
    </target>

          <path id="test.c">
                  <fileset dir="${ws.jars}" includes="*.jar"/>
          </path>
         
            <target name="makexsltreports">
                  <mkdir dir="${ws.home}/XSLT_Reports/output"/>

       <xslt in="${ng.result}/testng-results.xml" style="src/xslt/testng-results.xsl"
                       out="${ws.home}/XSLT_Reports/output/index.html"                classpathref="test.c" processor="SaxonLiaison">
                      <param name="testNgXslt.outputDir" expression="${ws.home}/XSLT_Reports/output/"/>
       <param name="testNgXslt.showRuntimeTotals" expression="true"/>
                  </xslt>
              </target>

    <!-- ****************** targets not used ****************** -->
</project>




NOTE: 1. instead of "F:\1" give your own jar files path
                 2. Instead of "testng.xml" replace your xml 


6. Create testng.xml file in your project with the following script for TestNG execution




<?xml version="1.0" encoding="UTF-8"?>                       
<suite name="Ant Suite">                                               
       <test name="SP Kandua">                                      
        <classes>                                                               
          <class name="sample.Xlstreport" ></class>         
        </classes>                                                              
       </test>                                                                    
 </suite>                                                                         


7. Now run from the command prompt

 > Goto your project directory

    
   ant makexsltreports

   We will get results like below image