Thursday 30 April 2015

Automation script, Navigate to "facebook.com" and select the Date of birth.



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;


public class ListBoxDemo {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();

driver.get("http://facebook.com");

WebElement ele1 = driver.findElement(By.id("month"));
WebElement ele2 = driver.findElement(By.id("day"));
WebElement ele3 = driver.findElement(By.id("year"));

Select sel = new Select(ele1);

sel.selectByVisibleText("Jul");

sel=new Select(ele2);

sel.selectByValue("26");

sel= new Select(ele3);

sel.selectByVisibleText("1991");

}

}

Tuesday 28 April 2015

Working with "Multi Select list box"

package WebdriverDemo2;

import java.util.List;

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;

public class MultiSelectDemo {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();

driver.get("http://www.plus2net.com/html_tutorial/html_frmddl.php");

WebElement wele = driver.findElement(By.xpath("//select[@name='ddl' and @multiple='']"));

Select slt = new Select(wele);

boolean flag = slt.isMultiple();
List<WebElement> lst = slt.getOptions();

int count=lst.size();

if(flag)
{
for(int i=0;i<count;i++)
{
slt.selectByIndex(i);
}
}

}

}

Monday 27 April 2015

Login to facebook with invalid login credentials and verify error message.

package WebdriverDemo2;

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

public class FBInvalidCredentials {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://fb.com");

String ExpectedRes = "Incorrect Email";

driver.findElement(By.id("email")).sendKeys("dcduhu");
driver.findElement(By.id("pass")).sendKeys("cdyghdyg");
driver.findElement(By.xpath("//input[@value='Log In']")).click();

String ActualRes = driver.findElement(By.xpath("//div[contains(text(),'Incorrect Email')]")).getText();

if(ExpectedRes.equals(ActualRes))
{
System.out.println("Text check complete--> PASS!");
}
else
System.out.println("Text check not complete--> FAIL!");

}

}

Friday 24 April 2015

Script for discarding a mail from drafts in GMAIL.

package WebdriverDemo2;

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

public class GmailDraftsDiscard {

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://accounts.google.com/ServiceLogin?sacu=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&hl=en&service=mail");

driver.findElement(By.id("Email")).sendKeys("manju.naidu13@gmail.com");

driver.findElement(By.id("Passwd")).sendKeys("***********");

driver.findElement(By.id("signIn")).click();

Thread.sleep(40000);

driver.findElement(By.xpath("//a[contains(text(),'Drafts')]")).click();

Thread.sleep(500);

driver.findElement(By.xpath("//tr[td[span[@title='Sun, Dec 7, 2014 at 7:52 PM']]]/td/div/div[@class='T-Jo-auh']")).click();

Thread.sleep(300);

driver.findElement(By.xpath("//div[contains(text(),'Discard drafts') and @role='button']")).click();


}

}

Selenium RC Vs. Webdriver



Selenium uses JavaScript to automate web pages. This lets it interact very tightly with web content, and was one of the first automation tools to support Ajax and other heavily dynamic pages. However, this also means Selenium runs inside the JavaScript sandbox. This means you need to run the Selenium-RC server to get around the same-origin policy, which can sometimes cause issues with browser setup.

WebDriver on the other hand uses native automation from each language. While this means it takes longer to support new browsers/languages, it does offer a much closer ‘feel’ to the browser. If you’re happy with WebDriver, stick with it, it’s the future. There are limitations and bugs right now, but if they’re not stopping you, go for it.
Selenium Benefits over WebDriver
* Supports many browsers and many languages, WebDriver needs native implementations for each new languagte/browser combo.

* Very mature and complete API
* Currently (Sept 2010) supports JavaScript alerts and confirms better
  Benefits of WebDriver Compared to Selenium
* Native automation faster and a little less prone to error and browser configuration
* Does not Requires Selenium-RC Server to be running
* Access to headlessHTMLUnit can allow really fast tests
* Great API

Script for "ActiTIME" application. Operation: Login into actitime application and logout.

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


public class WebDriverDemo {

public static void main(String[] args) throws InterruptedException {

WebDriver driver=new FirefoxDriver();
driver.get("http://hunter/login.do");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");

driver.findElement(By.id("loginButton")).click();

Thread.sleep(4000);

driver.findElement(By.className("logout")).click();

driver.close();

System.out.println("PASS!");

}

}

Script for logging into gmail and logout.

package WebdriverDemo2;

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


public class WebDriverDemo2 {

public static void main(String[] args) throws InterruptedException {
WebDriver driver= new FirefoxDriver();
driver.get("http://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("manju.naidu13@gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("***********");
driver.findElement(By.id("signIn")).click();
Thread.sleep(40000);
driver.findElement(By.xpath("//div[@id='gb']/div/div/div/div[2]/following-sibling::div/div/a/span")).click();
Thread.sleep(600);
driver.findElement(By.xpath("//a[contains(text(),'Sign out')]")).click();
System.out.println("Login---> Logout Pass!!");

}

}