Selenium Automation Lab Programs
Lab Exercise 3 – Program to Login into a webpage
package webdrivercommand;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Loginwebpage {
public static void main(String[] args) throws
InterruptedException {
//To Access ChromeBrowser we need a Chrome Driver
//set System Property
System.setProperty("webdriver.chrome.driver",
"C://Selenium//chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.google.com/");
//Wait for 5 seconds=5000ms
Thread.sleep(5000);
//close the browser
driver.close();
}
}
Lab Exercise 4 – Program to verify whether a test case
passed / failed
package webdrivercommand;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
//import jxl.write.Label;
public class Login {
public static void main(String[] args) throws
InterruptedException {
//Launch Chrome Browser
System.setProperty("webdriver.chrome.driver","C://Sel
enium//chromedriver.exe");
WebDriver driver=new ChromeDriver();
//To navigate to URL
driver.get("http://www.way2sms.com/");
Thread.sleep(5000);
driver.findElement(By.id("mobileNo")).sendKeys("97003
39582");
Thread.sleep(5000);
driver.findElement(By.name("password")).sendKeys("han
uman");
Thread.sleep(5000);
driver.findElement(By.xpath("//b[@id='mainErr']/follo
wing::*[5]")).click();
Thread.sleep(5000);
//Verification Point
String o="http://www.way2sms.com/send-sms";
String z=driver.getCurrentUrl();
if(z.equals(o))
{
System.out.println("Login Sucessful_Test
Passed");
}
else
{
System.out.println("Login
UnSucessful_TestFailed");
}
//Close Site
driver.close();
}
}
Lab Exercise 5 – Program to display the count of the
total number of web hyperlinks on a webpage and
listing them in order
package webdrivercommand;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Totalnoofobjects {
public static void main(String[] args) throws
InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\Sel
enium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Thread.sleep(5000);
driver.get("http://demo.guru99.com/test/newtours//");
Thread.sleep(5000);
List<WebElement> e =
driver.findElements(By.tagName("a"));
System.out.println(e.size());
for(int i=0; i<e.size(); i=i+1){
System.out.println(e.get(i).getText());
}
driver.close();
}}
Lab Exercise 6 – Program to display the total number of
elements in a combo box
package webdrivercommand;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Totalnoofobjectscombobox {
public static void main(String[] args) throws
InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C://Sel
enium//chromedriver.exe");
WebDriver driver=new ChromeDriver();
//To navigate to URL
driver.get("http://demo.guru99.com/test/newtours//");
Thread.sleep(5000);
//Syntax:
//objectName.Method(StaticClass.Locator(
"value")).OperationMethod();
//Click on Register Link
driver.findElement(By.linkText("REGISTER")).click();
Thread.sleep(5000);
//Pass text into Textbox for FirstName
driver.findElement(By.name("firstName")).sendKeys("Ma
dhwaraj");
Thread.sleep(5000);
driver.findElement(By.name("lastName")).sendKeys("Pro
f");
Thread.sleep(5000);
driver.findElement(By.name("phone")).sendKeys("970333
6699");
Thread.sleep(5000);
driver.findElement(By.id("userName")).sendKeys("abcd@
gmail.com");
Thread.sleep(5000);
driver.findElement(By.name("address1")).sendKeys("che
nnai fasf");
Thread.sleep(5000);
//driver.findElement(By.name("address2")).sen
dKeys("chennai fasf");
//Thread.sleep(5000);
driver.findElement(By.name("city")).sendKeys("chennai
");
Thread.sleep(5000);
driver.findElement(By.name("state")).sendKeys("TN");
Thread.sleep(5000);
driver.findElement(By.name("postalCode")).sendKeys("5
000221");
Thread.sleep(5000);
//Select Class in SWD
Select s=new
Select((driver.findElement(By.name("country"))));
//Get all the option from dropdown list and
assign into List
List <WebElement> listOptionDropdown =
s.getOptions();
// Count the item dropdown list and assign
into integer variable
int dropdownCount =
listOptionDropdown.size();
//Print the total count of dropdown list
using integer variable
System.out.println("Total Number of item
count in dropdown list = " + dropdownCount);
//selectByIndex();
//selectByVisibleText("INDIA");
s.selectByVisibleText("INDIA");
Thread.sleep(5000);
driver.findElement(By.id("email")).sendKeys("abcd@gma
il.com");
Thread.sleep(5000);
driver.findElement(By.name("password")).sendKeys("Mad
hvaraj");
Thread.sleep(5000);
driver.findElement(By.name("confirmPassword")).sendKe
ys("Madhvaraj");
Thread.sleep(5000);
//Image Button
//driver.findElement(By.name("Submit")).click
();
//Thread.sleep(5000);
//close browser
driver.close();
}
Lab Exercise 7 – Program to automate the switching
between two websites and performing some actions
with them, displaying the actions using Mozilla web
browser
package webdrivercommand;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BNCommands {
public static void main(String[] args) throws
InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C://Sel
enium//chromedriver.exe");
WebDriver driver = new ChromeDriver();
//To Open URL in Browser
driver.get("https://www.google.com/");
//getCurrentUrl(); is a method to get URL of
Current Page in the Browser
String baseURL=driver.getCurrentUrl();
System.out.println(baseURL);
Thread.sleep(5000);
driver.navigate().to("https://seleniummaster.com/llc/
");
String toURL=driver.getCurrentUrl();
System.out.println(toURL);
Thread.sleep(5000);
driver.navigate().back();
String backURL=driver.getCurrentUrl();
System.out.println(backURL);
Thread.sleep(5000);
driver.navigate().refresh();
String refreshURL=driver.getCurrentUrl();
System.out.println(refreshURL);
Thread.sleep(5000);
driver.navigate().to("https://seleniummaster.com/llc/
");
String toURL1=driver.getCurrentUrl();
System.out.println(toURL1);
Thread.sleep(5000);
//Maximize Window
/* To maximize or minimize window */
driver.manage().window().maximize();
Thread.sleep(5000);
driver.close();
}
Lab Exercise 8 – Program to automate the reading of
data from an excel file and displaying the data on the
eclipse console output window
package webdrivercommand;
import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcelFile {
public void readExcel() throws BiffException,
IOException {
// TODO Auto-generated method stub
String FilePath =
"C:\\Selenium\\sampledoc.xls";
FileInputStream fs = new
FileInputStream(FilePath);
Workbook wb = Workbook.getWorkbook(fs);
// TO get the access to the sheet
Sheet sh = wb.getSheet("Sheet1");
// To get the number of rows present in sheet
int totalNoOfRows = sh.getRows();
// To get the number of columns present in
sheet
int totalNoOfCols = sh.getColumns();
for (int row = 0; row < totalNoOfRows; row++)
{
for (int col = 0; col < totalNoOfCols;
col++) {
System.out.print(sh.getCell(col,
row).getContents() + "\t");
}
System.out.println();
}
}
public static void main(String args[]) throws
BiffException, IOException {
ReadExcelFile DT = new ReadExcelFile();
DT.readExcel();
}
}