Thursday 27 August 2015

How to run selenium code on different browsers?


package pack2;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class sample_class {

public static void main(String[] args) {

//==========Sample code to execute on Firefox browser=============
FirefoxDriver d = new FirefoxDriver();

d.get("www.google.com");
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
d.findElement(By.name("q")).sendKeys("Testing Masters Technologies");
d.findElement(By.name("btnG")).click();
d.quit();

//===========Sample code to execute on Chrome browser=============
System.setProperty("webdriver.chrome.driver", "d:\\ChromeDriver.exe");
WebDriver d = new ChromeDriver();

d.get("www.google.com");
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
d.findElement(By.name("q")).sendKeys("Testing Masters Technologies");
d.findElement(By.name("btnG")).click();
d.quit();

//===========Sample code to execute on IE browser=================
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability (InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
System.setProperty("webdriver.ie.driver", "d:\\IEDriverServer.exe");
WebDriver d = new InternetExplorerDriver();

d.get("www.google.com");
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
d.findElement(By.name("q")).sendKeys("Testing Masters Technologies");
d.findElement(By.name("btnG")).click();
d.quit();

   }
}

Commonly used basic commands in Selenium

Below are some basic commands of selenium which you can try and use while writing your script for web application.

1.driver.get("http://www.google.com"); To open an application
2.driver.findElement(By.id("passwd-id")); Finding Element using Id
3.driver.findElement(By.name("passwd")); Finding Element using Name
4.driver.findElement(By.xpath("//input[@id=’passwd-id’]")); Finding Element using Xpath 5.element.sendKeys("some text"); To type some data
6.element.clear(); clear thecontents of a text field or textarea
7.driver.findElement(By.xpath("//select")); Selecting the value
8.select.findElements(By.tagName("option")); Selecting the value
9.select.deselectAll(); This will deselect all OPTIONs from the first SELECT on the page 10.select.selectByVisibleText("Testing Masters"); select the OPTION withthe displayed text of "Testing Masters"
11.findElement(By.id("submit")).click(); To click on Any button/Link
12.driver.switchTo().window("windowName"); Moving from one window to another window 13.driver.switchTo().frame("frameName"); swing from frame to frame (or into iframes)
14.driver.switchTo().frame("frameName.0.child"); to access subframes by separating the path with a dot, and you can specify the frame by itsindex too.
15.driver.switchTo().alert(); Handling Alerts
16.driver.navigate().to("http://www.example.com"); To Navigate Paeticular URL
17.driver.navigate().forward(); To Navigate Forward
18.driver.navigate().back(); To Navigate Backword
19.driver.close(); Closes the current window
20.driver.quit(); Quits the driver and closes every associated window.
21.driver.switch_to_alert(); Switches focus to an alert on the page.
22.driver.refresh(); Refreshes the current page.
23.webdriver.manage().window().setPosition(new Point(-2000, 0); to minimize browser window