Showing posts with label Selenium-WebDriver. Show all posts
Showing posts with label Selenium-WebDriver. Show all posts

Monday, June 2, 2014

Drag and Drop or how to use dragAndDrop() method.

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Dropdrag {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://yuiblog.com/sandbox/yui/3.2.0pr1/examples/dd/groups-drag_clean.html");
        Actions act = new Actions(driver);
        WebElement source1 = driver.findElement(By.id("pt1"));
        WebElement target1 = driver.findElement(By.id("t2"));
        act.dragAndDrop(source1,target1).perform();
       
       
    }

}

Friday, May 23, 2014

How to count the number of checkboxes checked in selenium webdriver ? or How to select multiple check box and verify ?

Note- Here 1st alternative checkbox has been selected, then it has been verified that which checkbox is selected and which is not selected.

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MultipleCheckBox {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.gsmarena.com/samsung-phones-9.php");
        List<WebElement> checkBoxes = driver.findElements(By.xpath("//input[@type='Checkbox']"));
        for(int i=0; i<checkBoxes.size(); i=i+2){
            checkBoxes.get(i).click();
        }
        int checkedCount=0, uncheckedCount=0;
        for(int i=0; i<checkBoxes.size(); i++){
            System.out.println(i+" checkbox is selected "+checkBoxes.get(i).isSelected());
            if(checkBoxes.get(i).isSelected()){
                checkedCount++;
            }else{
                uncheckedCount++;
            }
        }
        System.out.println("number of selected checkbox: "+checkedCount);
        System.out.println("number of unselected checkbox: "+uncheckedCount);
    }
}



output-
0 checkbox is selected true
1 checkbox is selected false
2 checkbox is selected true
3 checkbox is selected false
4 checkbox is selected true
5 checkbox is selected false
6 checkbox is selected true
7 checkbox is selected false
8 checkbox is selected true
9 checkbox is selected false
number of selected checkbox: 5
number of unselected checkbox: 5

Sunday, May 18, 2014

How to delete cookies.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Cookies {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.co.in/");
        System.out.println("cookies before delete: "+driver.manage().getCookies());
        driver.manage().deleteAllCookies(); //
        System.out.println("cookies after delete: "+driver.manage().getCookies());
    }
}

How to handle calender popup or date picker ?

Note- 1st click on the calendar button then click on the date which you want to select.
Here in the below example, it will always select the current date , you can pass any date which you want to select as per your requirement.


import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

    public class TodayDate {
    public static void main(String[] args) {
        int day, month, year;
          GregorianCalendar date = new GregorianCalendar();
          day = date.get(Calendar.DAY_OF_MONTH);
          month = date.get(Calendar.MONTH)+1;
          year = date.get(Calendar.YEAR);
          String today = "a_"+year+"_"+month+"_"+day;
          System.out.println(today);
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get("http://www.yatra.com/");
    driver.findElement(By.id("BE_flight_depart_date")).click();
    driver.findElement(By.xpath("//a[@id='"+today+"']")).click();
   
    }

    }

There are 100 testcases i want to execute only 3 test cases without using testng groups?

Note- just include those methods in testng.xml which you want to execute.

For ex- inside com.CC.scripts.Login class there are 100 test cases but you want to execute only 3 methods- at_loginValid, at_loginBlank and at_loginInValid. In that case you can edit your xml as below-

<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="com.CC.scripts.Login">
<methods>
<include name="at_loginValid"></include>
<include name="at_loginInValid"></include>
<include name="at_loginBlank"></include>
</methods>
</class>
</classes>
</test>
</suite>


Login class- Here hello method will not execute by using above testng.xml

package com.CC.scripts;
import org.testng.annotations.Test;

public class Login {
    @Test
    public void at_loginValid(){
        System.out.println("at_loginValid");
    }
    @Test
    public void at_loginInValid(){
        System.out.println("at_loginInValid");
    }
    @Test
    public void at_loginBlank(){
        System.out.println("at_loginBlank");
    }
    @Test
    public void hello(){
        System.out.println("hello");
    }
}

Saturday, May 17, 2014

What is the alternate way to send text in textbox of webpage with out using sendKeys() method ?

Note- Use Javascript to send text.

syntax-

((JavascriptExecutor)driver).executeScript("document.getElementById('attribute value of id').value='text which you want to pass'");

ex-
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WithoutSendKeys {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://accounts.google.com/ServiceLogin?sacu=1&scc=1...");
((JavascriptExecutor)driver).executeScript("document.getElementById('Email').value='sanjay'");
}
}

Thursday, May 15, 2014

How to change the position of the open browser window.

Note - use command -> driver.manage().window().setPosition(new Point(int value1,int value2));

import java.awt.AWTException;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MoveWindow
{
public static void main(String[] args) throws InterruptedException, AWTException
{
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().setPosition(new Point(0,0)); //move the window to top right corner
    Thread.sleep(2000);
    driver.manage().window().setPosition(new Point(500,400)); //move the window to 500unit horizontally and 400unit vertical
}
}

How to change the size of the open browser window.

Use the command- driver.manage().window().setSize(new Dimension(int value1, int value2));
where driver is WebDriver type.

ex-

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ResizeWindow {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().setSize(new Dimension(400,600));
    }
}

Wednesday, May 14, 2014

How to execute the testcases parallely in TestNG

Test cases can be run parallel by updating testng.xml.

ex- Here two browsers will open as thread-count = "2" (you can change it according to your requirement). In one browser all the classes will be executed which are under 1st test and in 2nd browser, all the classes will be executed which are under 2nd test.

<suite name="Suite" parallel="tests" thread-count="2">
  <test name="Test1">
    <classes>
      <class name="Library.Module1"/>
    </classes>
  </test>
  <test name="Test2">
    <classes>
      <class name="Library.Module2"/>
    </classes>
  </test>
</suite>


Go back to Interview Ques-Ans for Automation Tester

Go to Example with Real Scenario

Tuesday, May 13, 2014

How to download pdf file in desired location without using AutoIt tool.

Note- This code will download the pdf file inside 'C' drive. You can change the directory by changing the path here. profile.setPreference( "browser.download.dir", "download location path" )

import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;

public class DownloadPdf {
    public static void main(String[] args) {
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference( "browser.download.folderList", 2 );
        profile.setPreference( "browser.download.dir", "C:\\" ); //this will download pdf inside 'C' driver. You can give your path where u want to save the file.
        profile.setPreference( "plugin.disable_full_page_plugin_for_types", "application/pdf" );
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",  
        "application/csv,text/csv,application/pdfss, application/excel" );
        profile.setPreference( "browser.download.manager.showWhenStarting", false );
        profile.setPreference( "pdfjs.disabled", true );
       
        WebDriver driver = new FirefoxDriver(profile);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       
        //login
        driver.get("http://myaccount.rinfra.com/myaccnt/login.do");
        driver.findElement(By.id("username")).sendKeys("150811386");
        driver.findElement(By.name("password")).sendKeys("150811386");
        driver.findElement(By.xpath("//input[@alt='Enter']")).click();
       
       
        driver.findElement(By.xpath("//div[text()='close']")).click(); //close pop-up window
       
        Actions act = new Actions(driver);
        WebElement ele = driver.findElement(By.xpath("//span[contains(text(),'bill & payments')]"));
        act.moveToElement(ele).perform(); //move cursor over bill & payment
        driver.findElement(By.xpath("//a[text()='download / print bill']")).click(); //clicking on 3rd element in the list
        driver.findElement(By.xpath("//a[@class='jqTransformSelectOpen']")).click(); // clicking on select drop down     
        driver.findElement(By.xpath("//a[@index='1']")).click();     //selecting 1st element        
        driver.findElement(By.xpath("//img[@src='images/view.gif']")).click(); // clicking on view
       
        //handle the windows
        Iterator<String> it = driver.getWindowHandles().iterator();
        String mainPage = it.next();
        String child = it.next();
        driver.switchTo().window(child); //switch to child window
        driver.findElement(By.xpath("//button[@id='download']")).click(); //click on download button
        driver.quit();
    }
}

SVN(Sub Version) -Test Script repository

SVN is a repository (Test Script repository). Usually SVN server is installed in lab server and to this server, Access will be given to Automation Test Engineer.

Steps to download SVN server-

Step1- open this link - http://www.visualsvn.com/server/download/
Step2- click on download now.












Step3- click on save.



Step4- Now click on the downloaded file and follow the install instruction (just click on next next).



Step5- After installation, SVN server will launch by default if not then launch it. Then right click on Repositories and click on Create New Repository.











Step6- Give the Repository name. (for ex- give Repository name as SpiceJet.) then click on next.





Step7- Select Single-project repository and then click on next.




Step8- Then click on Create




Step9- Click on finish.



Step10- Go to user --> Right click --> create user --> give username and password then click on OK.






Note- Now Go to eclipse. In order to integrate eclipse with SVN server. It is required to download another plugin called as Subclipse in eclipse.

Steps to download Subclipse

Step1- Open Eclipse -> go to Help -> click on Eclipse Marketplace



Step2- in Find textbox type Subclipse and click on Go.



Step3-  Under Subclipse click on install.




Step4- Just click on next next and restart the eclipse after installation.
Step5- Now go to Window in eclipse -> show view -> click on Other



Step6- Expand SVN -> select SVN Repositories -> click on OK.



Step7- Under SVN Repositories in menu bar, click on Add SVN Repositoy.





Give the Url.

Note- To get the Url, goto SVN Server, select the created workspace -> right click -> Copy URL to Clipboard -> this will copy the Url -> now paste this Url to the above Url text box.




Step8- After pasting the Url , click on Finish.


Step9- That's all. You are done with the setup.
Step10- Create a project in eclipse. In order to upload all the files to SVN server, select the project -> right click -> go to Team -> click on Share Project




Step11- Select SVN and click on Next.




Step12- select 'Use existing repository locations' and select the repository to which you want to add your project and click on next.




Step13- Click on finish.




Step14- After wait for a while, it has added your project to SVN server. Go back to SVN server and refresh it, you will find your project name there.

Step15- Now just right click on any file/package of the Project -> go to Team -> click on Commit. -> Click on OK.

For ex- Here Owler is the Project and library is the file which has been added to SVN.





Step16- Finally added the project to SVN server. The item which has been added to SVN server, it will look like below.







Please leave your valuable comment below.!! Thanks!!