Vous êtes sur la page 1sur 7

These are generic ways to do certain common task i.e.

Id: WebElement element = driver.findElement(By.id(""));


Name: WebElement element = driver.findElement(By.name(""));
Tag Name: WebElement frame = driver.findElement(By.tagName("iframe"));
Xpat
<fieldset>
<input type="hidden" value="partner" name="source">
<label class="block clearfix">
<span class="block input-icon input-icon-right">
CSS: WebElement element = driver.findElement(By.CSS.name(""));
LinkText: WebElement element = driver.findElement(By.LinkText.(""));
I hope these commands will let you work with Selenium WebDriver like a star. Ple
ase leave us a comment and share your own experiences, commands with us.
Testing 0 Comments

Popup Windows and Frames
Popup window handling is one of the must handle things in Automation and Webdriv
er does is really well. Using the window handle command.
String winHandleBefore = driver.getWindowHandle(); for(String winHandle : driver
.getWindowHandles()){ driver.switchTo().window(winHandle); }
1
2
3
4
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
Once action is performed, switch back to parent window.
driver.switchTo().window(driver.getWindowHandle());
1
driver.switchTo().window(driver.getWindowHandle());
List Box
Select an item in Listbox using Selenium Webdriver is quite easy, there two ways
to do it.
WebElement select = driver.findElement(By.id("selection")); List&lt;WebElement&g
t; options = select.findElements(By.tagName("option")); for (WebElement option :
options) { if("Germany".equals(option.getText())) option.click(); }
1
2
3
4
5
6
WebElement select = driver.findElement(By.id("selection"));
List&lt;WebElement&gt; options = select.findElements(By.tagName("option"));
for (WebElement option : options) {
if("Germany".equals(option.getText()))
option.click();
}
Or
WebElement roleDropdown = driver.findElement(By.id(""); roleDropdown.click();
1
2
WebElement roleDropdown = driver.findElement(By.id("");
roleDropdown.click();
Then
WebElement roleOptionOne = driver.findElement(By.id(FIRST_OPTION)); roleOptionOn
e.click();
1
2
WebElement roleOptionOne = driver.findElement(By.id(FIRST_OPTION));
roleOptionOne.click();
Right Click
WebElement webelement = driver.findElement(By.xpath("*Xpath Locator*")); Actions
actions = new Actions(driver); Action action = actions.contextClick(webelement)
.build(); action.perform(); <h2>Double Click</h2> Actions action = new Actions(d
river); action.doubleClick(myElemment); action.perform();
1
2
3
4
5
6
7
8
9
WebElement webelement = driver.findElement(By.xpath("*Xpath Locator*"));

Actions actions = new Actions(driver);
Action action = actions.contextClick(webelement).build();
action.perform();
<h2>Double Click</h2>
Actions action = new Actions(driver);
action.doubleClick(myElemment);
action.perform();
Excel Reader
Read or write data from excel using the jxl or POI, I will prefer POI. Create a
property reader file then read the data
Workbook workbook = WorkbookFactory.create(new FileInputStream(file)); Sheet she
et = workbook.getSheetAt(0);
1
2
Workbook workbook = WorkbookFactory.create(new FileInputStream(file));
Sheet sheet = workbook.getSheetAt(0);
Database Connection
To connect to Database using WebDriver with Java, we use DBC(Java Database Connec
tivity) API.
DriverManager.getConnection(URL, "username", "password" )
1
DriverManager.getConnection(URL, "username", "password" )
Time Outs Wait
Implicit Waits:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
1
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Or
Thread.sleep(1000);
1
Thread.sleep(1000);
Explicit Waits
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.un
til(ExpectedConditions.elementToBeClickable(By.id("someid"))); <h2>Alert Box</h2
> driver.findElement(By.id("updateButton")).click(); Alert alert = driver.switch
To().alert();
1
2
3
4
5
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("s
omeid")));
<h2>Alert Box</h2>
driver.findElement(By.id("updateButton")).click();
Alert alert = driver.switchTo().alert();
alert.accept(); or alert.dismiss();
Navigation
driver.navigate().to(www.google.com); driver.get("http://www.google.com"); drive
r.navigate().forward(); driver.navigate().back(); driver.navigate().refresh(); d
river.manage().deleteAllCookies(); driver.close();
1
2
3
4
5
6
7
driver.navigate().to(www.google.com);
driver.get("http://www.google.com");
driver.navigate().forward();
driver.navigate().back();
driver.navigate().refresh();
driver.manage().deleteAllCookies();
driver.close();
Table Columns
WebElement Table = driver.findElement(By.id(""))); List&lt;WebElement&gt; Row =
Table.findElements(By.tagName("tr"));
1
2
WebElement Table = driver.findElement(By.id("")));
List&lt;WebElement&gt; Row = Table.findElements(By.tagName("tr"));
Drag And Drop
WebElement element = driver.findElement(By.name("source")); WebElement target =
driver.findElement(By.name("target")); (new Actions(driver)).dragAndDrop(element
, target).perform();
1
2
3
4
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();
Mouse Over
Actions actions = new Actions(driver); WebElement mouseover = driver.findElement
(By.id("")); actions.moveToElement(mouseover); actions.click().perform();
1
2
3
4
Actions actions = new Actions(driver);
WebElement mouseover = driver.findElement(By.id(""));
actions.moveToElement(mouseover);
actions.click().perform();
IsElement/TextPresent
There are various way to check weather an element is present of not before execu
tion of a piece of code.
Option 1
private boolean isElementPresent(WebDriver driver, By by){ try{ driver.findEleme
nt(by); return true; }catch(NoSuchElementException e){ return false; } }
1
2
3
4
5
6
7
8
private boolean isElementPresent(WebDriver driver, By by){
try{
driver.findElement(by);
return true;
}catch(NoSuchElementException e){
return false;
}
}
Option 2
if (driver.findElements(locator).size() &gt; 0) { return true; } else { return f
alse; } }
1
2
3
4
5
6
if (driver.findElements(locator).size() &gt; 0) {
return true;
} else {
return false;
}
}
Option 3
Sweet and simple one line of code
Webelement elementpresent = driver.PageSource.Contains("Sign out")
1
Webelement elementpresent = driver.PageSource.Contains("Sign out")
Web Elements Finding
These are generic ways to do certain common task i.e.
Id: WebElement element = driver.findElement(By.id(""));
Name: WebElement element = driver.findElement(By.name(""));
Tag Name: WebElement frame = driver.findElement(By.tagName("iframe"));
Xpath: WebElement element = driver.findElement(By.xpath.name(""));
CSS: WebElement element = driver.findElement(By.CSS.name(""));
LinkText: WebElement element = driver.findElement(By.LinkText.(""));
I hope these commands will let you work with Selenium WebDriver like a star. Ple
ase leave us a comment and share your own experiences, commands with us.
Testing 0 Comments
Related Posts
site-for-free
A Guide to Building a Great Looking Site for Free
0 Comments
java
10 Must Know Java Programs for Intermediate Developers
0 Comments
Popular
Recent
Comments
banned-by-akismet
Are you banned by Akismet for spaming?
81 Comments
wordpress Posting Content via XML RPC
WordPress XMLRPC Posting Content From Outside WordPress Admin Panel
76 Comments
Disqus_comment_panel
Harness the power of Disqus comment system
40 Comments
php-python
PHP vs Python: Analysis
21 Comments
32-SEO-tips
32 SEO Tips to Boost your Website Traffic
16 Comments
@SuppressWarnings("resource")
Scanner luci= new Scanner(System.in);

String idInput;
System.out.println("TESTENACT");
String EmailInput;
System.out.println("ashim87@yahoo.co.in");
EmailInput=luci.nextLine();
String passwordInput;
System.out.println("1234");
passwordInput=luci.nextLine();
if( Account idInput equals("TESTENACT") EmailInput equals("ashim
@yahoo.co.in")passwordInput equals("1234")){
System.out.println("Authatanification Completed");
}else
{
System.out.println("Wrong AccountId or Email or Password
!");
}
}

Vous aimerez peut-être aussi