Selenium Web Driver Command List

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Selenium Web Driver Command List

Command                                               Description

driver.get("http://www.google.com"); To open an application


driver.findElement(By.id("passwd-id")); Finding Element using Id
driver.findElement(By.name("passwd")); Finding Element using Name
driver.findElement(By.xpath("//input[@id=’passwd-id’]")); Finding Element using Xpath
element.sendKeys("some text"); To type some data
element.clear(); clear thecontents of a text field or textarea
driver.findElement(By.xpath("//select")); Selecting the value
select.findElements(By.tagName("option")); Selecting the value
select.deselectAll(); This will deselect all OPTIONs from the first SELECT on the page
select.selectByVisibleText("Edam"); select the OPTION withthe displayed text of “Edam”
findElement(By.id("submit")).click(); To click on Any button/Link
driver.switchTo().window("windowName"); Moving from one window to another window
driver.switchTo().frame("frameName"); swing from frame to frame (or into iframes)
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.
driver.switchTo().alert(); Handling Alerts
driver.navigate().to("http://www.example.com"); To Navigate Paeticular URL
driver.navigate().forward(); To Navigate Forward
driver.navigate().back(); To Navigate Backword
driver.close() Closes the current window
driver.quit() Quits the driver and closes every associated window.
driver.switch_to_alert() Switches focus to an alert on the page.
driver.refresh() Refreshes the current page.
driver.implicitly_wait(30) Amount of time to wait
driver.set_script_timeout(30) The amount of time to wait
driver.get_screenshot_as_file('/Screenshots/foo.png') The full path you wish to save your
screenshot to
driver.get_screenshot_as_base64() Gets the screenshot of the current window as a base64
encoded string which is useful in embedded images in HTML
WebDriver with Excelsheet
Create the excel sheet in .xls format

//Webdriver with Excel


import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class WebDriverExcel {
private WebDriver driver;
@BeforeClass
public void Startup(){
driver = new FirefoxDriver();
}
@Test (description="OrangeHRM Login using excel sheet")
public void Login() throws Exception{
FileInputStream fi=new FileInputStream("E:\\Selenium\\LoginExcel.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
driver.get("http://127.0.0.1/orangehrm-2.6/login.php");
try
{
for (int i = 1; i < s.getRows(); i++)
{
//Read data from excel sheet
String s1 = s.getCell(0,i).getContents();
String s2 = s.getCell(1,i).getContents();
driver.findElement(By.name("txtUserName")).sendKeys(s1);
driver.findElement(By.name("txtPassword")).sendKeys(s2);
driver.findElement(By.name("Submit")).click();
Thread.sleep(3000);
if(driver.getTitle().equals("OrangeHRM"))
{
System.out.println("admin page Displayed successfully");
}
else
{
System.out.println("Do not Displayed");
}
driver.findElement(By.linkText("Logout"));
}
}
catch(Exception e)
{
System.out.println(e);
}
}
@AfterClass
public void teardown(){
driver.quit();
}
}

WebDriver with TestNG - Gmail Login Functionality


Below is the code for GMAIL Login functionality using WebDriver with TestNG

package com.test.webdriver;
import static org.testng.AssertJUnit.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
  
public class Driver {
private WebDriver driver;
 @BeforeClass
 public void Startup(){
  driver = new FirefoxDriver();
 }
 @Test (description="Google Login")
 public void GoogleLogin() throws Exception{
  driver.get("http://www.gmail.com");
  assertEquals("Sign in",
driver.findElement(By.id("signIn")).getAttribute("value"));
  driver.findElement(By.id("Email")).sendKeys("*********");
  driver.findElement(By.id("Passwd")).sendKeys("**********");
  driver.findElement(By.id("signIn")).click();
  Thread.sleep(10000);
  driver.switchTo().frame("canvas_frame");
  driver.findElement(By.id("gbgs4dn")).click();
  driver.findElement(By.id("gb_71")).click();
  driver.switchTo().defaultContent(); 
assertEquals("Sign in to Gmail",
driver.findElement(By.id("button")).getText());
 }
@AfterClass
 public void teardown(){
   driver.quit();
 }
}

Database Connection Using Selenium


Database Connection Using Selenium 

MS Access with Java through JDBC-ODBC connection


Microsoft has provided a method to build a quick Jet-Engine database on your computer without the
need for any specific database software (it comes standard with Windows). Using this method, we
can even create a blank Microsoft Access database without having MS Access installed!
MS Access data bases can be connected to via ODBC. Instead of accessing the database directly,
we can access it via a Data Source Name (DSN). Here's how to set up a DSN on your system:
Open Windows' ODBC Data Source Administrator as follows: 
In Windows 95, 98, or NT, choose Start > Settings > Control Panel, then double-click the ODBC
Data Sources icon. Depending on your system, the icon could also be called ODBC or 32bit ODBC. 
In Windows 2000, choose Start > Settings > Control Panel > Administrative Tools > Data Sources. 
In the ODBC Data Source Administrator dialog box, click the System DSN tab. 
Click Add to add a new DSN to the list. 
Scroll down and select the Microsoft Access (.MDB) driver 
Type in the name "TestDB" (no quotes, but leave the cases the same) for the Data Source Name 
Click CREATE and select a file to save the database to (I chose "d:\java\TestDB.mdb") - this creates
a new blank MS Access database! 
Click "ok" all the way out Now our data source is done! 
Here's a complete program showing how to access your new DSN data source:
package qtt.selenium;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import com.thoughtworks.*;
import com.thoughtworks.selenium.DefaultSelenium;
import org.junit.*;
import org.openqa.selenium.server.SeleniumServer;
public class DBtest
{
DefaultSelenium selenium;
SeleniumServer server;
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:TestDB";
String username = "";
String password = "";
@Before
public void setup()throws Exception
{
server = new SeleniumServer();
server.start();
selenium = new DefaultSelenium("127.0.0.1",4444,"*chrome","http://");
selenium.start();
}
@Test
public void test()throws Exception
{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
Statement st = conn.createStatement();
// st.executeUpdate("drop table survey;");
st.executeUpdate("create table suresh(id int,name varchar(30),Homesite varchar(30));");
st.executeUpdate("insert into suresh (id,name,Homesite) values (1,'SURESH','selenium-
suresh.blogspot.com')");
st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM suresh");
if(rs.next());
{
String homesite = rs.getString(3);
selenium.open(homesite);
selenium.windowMaximize();
Thread.sleep(30000);
}
}
@After
public void teardown() throws Exception
{
selenium.stop();
server.stop();
}
}

You might also like