Selenium Webdriver Scripts:Selenium Webdriver getTagName()

script 1

package selenium_examples;


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


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
String tagname =  driver.findElement(By.id("gbqfba")).getTagName();
System.out.println(tagname);

}
}

Output:-

button

getTagName() method Gets the tag name of the element.

Above script gets tag name of google search button.


script2

package selenium_examples;


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


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://www.yahoo.co.in");
String tagname =  driver.findElement(By.name("p")).getTagName();
System.out.println(tagname);

}
}

Output:-

input

Above script gets tag name of search textbox from yahoo.co.in website.

Selenium Webdriver Scripts : how to get browser name and version using selenium webdriver


package selenium_examples;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://www.funandknowledge.blogspot.com");
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();

String browserName = cap.getBrowserName().toLowerCase();
System.out.println(browserName);

String version = cap.getVersion().toString();
System.out.println(version);


}
}



Output :-

firefox
19.0

Selenium Tutorials : Record and Playback in selenium ide

Record a test script

In this tutorial we are going to record a test script with Selenium IDE.

1.Open mozilla firefox browser and in tools menu click on selenium ide.

2.Click on record button in selenium ide.

3.Open google.com in firefox browser.

4.Type funandknowledge.blogspot.com  in google search box and click on search button.

5.Stop recording in selenium by clicking on red button.

6.Click on play current testcase icon which playbacks the recording actions.

screenshot for above recorded scenario in Selenium Ide



How to download and install selenium IDE?




Steps to download and install Selenium IDE (Integrated Development Environment).

1.In Mozilla Firefox browser open http://docs.seleniumhq.org/download/

2.Under Selenium IDE section click on version number here its 2.3.0 as shown in screenshot below


3.Clicking on selenium ide link a security pop up box as shown in screenshot will display.Click on "Allow".



4.When you click on allow button, Firefox will install Selenium IDE.Click on restart button in pop up.



5.After firefox has restarted you can see Selenium IDE listed under the Firefox Tools menu.Click on Selenium IDE as shown in screenshot or press Ctrl+Alt+S it will launch Selenium IDE.













How to download and install UFT 11.5



Steps to download and install UFT 11.5 :-

1.open url http://www8.hp.com/us/en/software-solutions/software.html?compURI=1172122#tab=TAB3

2.Click on 'Trial software'

3.Click on HP Unified Functional Testing 11.50 CC English SW E-Media Evaluation



4.Fill in your personal details and click Next



5.Agree Software Download Terms of Use

 
6.Click on download link


7.It will take 2 to 3 hrs to download

8.Extract the zip file

9.Choose the default options and click on cancel when it asks for the license key

10.UFT 11.5 trial version will be successfully installed and can be used for 30 days

Selenium getAttribute(value) and getAttribute(maxlength) script examples



package selenium_examples;


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


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://book.spicejet.com/");
System.out.println(driver.findElement(By.name("ControlGroupSearchView_AvailabilitySearchInputSearchVieworiginStation1_CTXT")).getAttribute("value"));




}
}

Output:-

Leaving from...


getAttribute() method :- Gets the value of a given attribute of the element.

getAttribute("value") retrieves the value inside the fields.In above script from http://book.spicejet.com,it retrieves text from "Leaving from" Dropdown box .


Script 2

package selenium_examples;


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


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://ebay.in");
System.out.println(driver.findElement(By.id("gh-ac")).getAttribute("maxlength"));




}
}

Output:-

300

getAttribute("maxlength") retrieves the maxlength of the field.In above script from http://ebay.in ,it retrieves maxlength of search textbox .



How to capture label text in Selenium webdriver


Script to get label text or capture label text in Selenium webdriver

package selenium_examples;


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


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
System.out.println(driver.findElement(By.name("btnK")).getText());
System.out.println(driver.findElement(By.name("btnI")).getText());

 
}
}


Output:-

Google Search
I'm Feeling Lucky

Above scripts gets the text of buttons in google webpage.

getText() method gets text of the element.


How to get names of links in selenium webdriver?

Below script gets the links names of Signon and Register in Mercury Tours webpage.


package selenium_examples;


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


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
System.out.println(driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td/a")).getText());
System.out.println(driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/a")).getText());



}
}

Output:-

SIGN-ON
REGISTER

How to Get the Tooltip text through Selenium Webdriver


Webdriver script to get Tooltip text in selenium webdriver.

package selenium_examples;


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

public class Sample {

public static void main(String[] args)
   {

WebDriver driver = new FirefoxDriver();
   driver.get("http://docs.seleniumhq.org/");
   WebElement toolTipObject = driver.findElement(By.xpath("/html/body/div/div/h1/a"));
   String GoogleTooltip = toolTipObject.getAttribute("title");
        System.out.println(GoogleTooltip);
             }

           }

Output of the script:-

Return to Selenium home page


Above script opens http://docs.seleniumhq.org/ webpage and gets the tooltip of below highlighted part. imag



getAttribute():-Gets the value of an element attribute specified.


How to capture screenshot of webpage in selenium webdriver


package selenium_examples;


import java.io.File;
import java.io.IOException;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.apache.commons.io.FileUtils;

public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://funandknowledge.blogspot.com");

  File capturescreenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);


 try {
FileUtils.copyFile(capturescreenshotFile,new File("E:\\fnkblogscreenshot.png"));
} catch (IOException e) {

e.printStackTrace();
}

 
}
}

Above script takes screenshot of funandknowledge.blogspot.com webpage.Screenshot is stored in E drive with name fnkblogscreenshot.

How to select radiobutton in selenium webdriver


package selenium_examples;



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

import java.util.concurrent.TimeUnit;

Selenium webdriver script to select radiobutton.

public class example1 {

public static void main(String[] args)
   {

WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
driver.manage().timeouts().implicitlyWait(100,TimeUnit.SECONDS);
WebElement we = driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[2]/td[2]/b/font/input[2]"));
we.click();
System.out.println(we.isSelected());

}


}
   
Output of the above script:-

true


Above script selects One Way radio button in Flight Finder,Mercury Tours website(http://newtours.demoaut.com/).


Selenium Webdriver script to get data from textbox and clear the data


package selenium_examples;


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


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");

driver.findElement(By.id("gbqfq")).sendKeys("Selenium");

System.out.println(driver.findElement(By.id("gbqfq")).getAttribute("value"));

driver.findElement(By.id("gbqfq")).clear();
 
}
}

Output will be:-

Selenium


Above script opens google site and enters Selenium in search textbox and then captures data from the textbox and prints 'Selenium' in Console.
The clear() method deletes the text in text box.




Selenium Webdriver script to get all options from dropdown list


package selenium_examples;

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.support.ui.Select;

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

public class example1 {

public static void main(String[] args)
   {

WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
driver.manage().timeouts().implicitlyWait(100,TimeUnit.SECONDS);
   Select droplist = new Select(driver.findElement(By.name("fromPort")));

        droplist.selectByVisibleText("London");


WebElement we = driver.findElement(By.name("fromPort"));

List options =  we.findElements(By.tagName("option"));
for (WebElement option : options) {
System.out.println(option.getText());
}


}

}
   
Output will be :-

Acapulco
Frankfurt
London
New York
Paris
Portland
San Francisco
Seattle
Sydney
Zurich

Above script logins into Mercury tours website and selects value "London" from Departing From dropdown list and it displays all the options of dropdown list.


Selenium Webdriver script to click on link or button

selenium webdriver script to click on link or button

Script1

package selenium_examples;

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

public class Test {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
driver.findElement(By.xpath("/html/body/div/div[3]/div/div/div/div/ol/li[3]/a/span[2]")).click();
System.out.println("Successfully clicked on Images link");

}

}

Output will be :-

Successfully clicked on Images link


Above script clicks on Images link in google website.


Script 2

package selenium_examples;

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

public class Test {

public static void main(String[] args) {

   /*
        Simple script example to
        click on button in google website.
       
   */
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
driver.findElement(By.id("gbqfq")).sendKeys("Selenium");
driver.findElement(By.id("gbqfb")).click();
          //driver.findElement(By.xpath("/html/body/div/div[3]/div/div/div/div/ol/li[3]/a/span[2]")).click();



}

}

Above script enters Selenium in textbox and clicks on google search button.

Single line and Multiline comments in selenium webdriver:-

Singleline comment:-In above script line //driver.findElement(By.xpath("/html/body/div/div[3]/div/div/div/div/ol/li[3]/a/span[2]")).click(); line is comment.
Single line comment starts with // and ends at the end of the line.


Multiline comment:-

/*
        Simple script example to
        click on button in google website.
       
 */

This is a multiline comment which starts with /* and ends with */. All the lines between /* and */ will be treated as comments.



Selenium Webdriver Login Script

Selenium Webdriver login script

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;

public class example1 {

public static void main(String[] args)
   {

WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
driver.findElement(By.name("findFlights")).click();
System.out.println("Done!");

}
    }

Above script Opens mercury tours website and logins into the site with given credentials.

and clicks on continue button(with the default values) in Find a Flight  page.


Implicit wait is used for waiting.




How to check and uncheck checkbox with selenium webdriver

Selenium Webdriver checkbox checked n unchecked script

package selenium_examples;

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 spicejet_example {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

   driver.get("https://book.spicejet.com/Login.aspx");

   driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);

   WebElement we = driver.findElement(By.id("AvailabilitySearchInputLoginView_Defense"));

   we.click();

   System.out.println(we.isSelected());

   we.click();

   System.out.println(we.isSelected());


}
}

Output of the above script:-

true
false


Above script checks Checkbox,Indian armed forces personnel in https://book.spicejet.com/ website and unchecks the same checkbox.


isSelected() method verifies whether the element is selected or not.Used for checkboxes, options in a select and radio buttons.
It returns true if the element is currently selected or checked, false otherwise.



How to Maximize a firefox browser window using selenium webdriver

How to Maximize a firefox browser window using selenium webdriver?

Maximizing Browser window using Selenium webdriver script

package selenium_examples;


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

public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
driver.manage().window().maximize();
                driver.close();
}

}

Above script maximizes browser window and closes the browser.

First Selenium Webdriver Script


Script 1-

package selenium_examples;

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

public class example1 {

public static void main(String[] args)
   {

WebDriver driver = new FirefoxDriver();
driver.get("http://www.funandknowledge.blogspot.com");
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
driver.close();
}

    }


Output will be :-

QTP Scripts
http://www.funandknowledge.blogspot.in/


Webdriver methods used in above script:

driver.get - Takes you to the Website www.funandknowledge.blogspot.com
driver.getTitle - Returns The title of the current page
driver.getCurrentUrl - Returns The URL of the page currently loaded in the browser
driver.close - Closes the current window

Other webdriver methods mostly used:

driver.getPageSource - Returns the page source of the current page
driver.quit - quits driver and closes every associated window


Script 2-

package selenium_examples;


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

public class Sample {

public static void main(String[] args)
   {

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.findElement(By.id("gbqfq")).sendKeys("site:funandknowledge.blogspot.com");
driver.findElement(By.id("gbqfb")).click();
}

    }

Related Posts Plugin for WordPress, Blogger...