Skip to main content

Sample Programs using Selenium WebDriver

Sample Programs using Selenium WebDriver

In the previous post, We have given Installation and Setup steps to Webdriver , Now let’s see  some sample programs!

Sample1: To Login to gmail.com

  1. Open a Firefox browser
  2. Enter gmail.com
  3. Enter user name
  4. Click on Next Button
  5. Enter password
  6. Click on SignIn button
  7. Print success login message

Now we will automate the above scenario. In the previous post, I had discussed about creating a Java Project, Package, Class. And how to import the JAR files into the project. Here also, we have followed the same thing. For example, we will create a Project called “Gmail” , Package as “Mypackage”, Class as “main″. Now, we will write the code as below:

package MyPackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
public static void main(String[] args) {
FirefoxDriver driver=new FirefoxDriver();
driver.get(“https://www.gmail.com/”);
driver.findElement(By.id(“Email”)).sendKeys(“your username”);
driver.findElement(By.id(“next”)).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id(“Passwd”)).sendKeys(“your Password”);driver.findElement(By.id(“signIn”)).click();
System.out.println(“Login Success”);

gmailLogin

Sample2: Accessing Links using Selenium Webdriver

  1. Open Firefox
  2. Enter icicibank.com
  3.  Count number of links
  4.  Get the names of the links.

Now we will automate the above scenario, See the code below-

package MyPackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
public class linktesting

{

public static void main(String[] args) {

//Create a new instance of Firefox Browser
FirefoxDriver driver = new Firefoxdriver();

//Open the URL in firefox browser
driver.get(“https://www.icicibank.com/”);

//Maximize the Browser window
driver.manage().window().maximize();

//links assign into one variable
//take array list
//web element is data type
list<webelements>links=driver.findelements(by.tagName(“a”);

//to print number of links
System.out.println (links.size());

For(int i=0;i<links.size();i++)
{
System.out.println(links.get(i).getText());

}
}
}

When I execute the program, found 573 links, Printing the same links. See the snapshot, you can see few of them, scroll down will list all.

links

 
So, We have seen how to login to gmail and how to count links in a website, Will see more programs and Selenium Webdriver features in Next post, Stay tuned to etestinghub.com :)