Professional Documents
Culture Documents
Please copy the below is the sample code for Table and make an html file.
<!DOCTYPE html>
<html>
<head>
<style>
tbody {color:red;}
table, th, td { border: 1px solid black;}
</style>
</head>
<body>
<p>Dynamic Table elements</p>
<p><b>Tip:</b> The tbody elements will not affect the layout of the table by def
ault. However, you can use CSS to style these elements.</p>
<table>
<tbody>
<tr>
<td>2014</td>
<td>January</td>
<td>$100</td>
<td>Tk.8000</td>
</tr>
</tr>
<tr>
<td>April</td>
<td>$50</td>
</tr>
<tr>
<td>2015</td>
<td>February</td>
<td>$80</td>
<td>Tk.6400</td>
</tr>
<tr>
<td>March</td>
<td>$60</td>
<td>Tk.4800</td>
</tr>
</tbody>
</table>
</body>
</html>
Java Code
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;
public class Dynamictablehandler {
public static void main(String[] a) throws InterruptedException {
// Initialize Web driver
WebDriver driver = new FirefoxDriver();
//Maximize browser window
driver.manage().window().maximize();
//Go to Page
driver.get("file:///F:/table.html");
//get the entire html table and store this in a variable
WebElement table = driver.findElement(By.xpath("html/body/table/tbody"));
//Get all the rows
List<webelement> rows = table.findElements(By.tagName("tr"));
for (int r = 0; r < rows.size(); r++) {
//Get all the columns in every row
List<WebElement> columns = rows.get(r).findElements(By.tagName("td"));
for (int col = 0; col < columns.size(); col++) {
System.out.print(columns.get(col).getText().trim() + " ");
}
System.out.println();
}
driver.quit();
}
}
-----------------------------The Easy way of uploading a file is simple case of just finding the element and
typing the absolute path of the document into it.
It is mandatory that it works only when the textbox is enabled. So please make s
ure that the input element is visible.
Sample HTML Code should look similar to this :
<html>
<body>
<form enctype="multipart/form-data" action="parse_file.php" method="post">
<p>Browse for a file to upload: </p>
<input type="file" name="uploadsubmit">
<br/><br/>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>
The following is the example program to upload a file using sendKeys() in seleni
um webdriver without using any third party tools:
package com.easy.upload;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class UploadFile {
static WebDriver driver;
String URL = "application URL";
@Test
public void testUpload() throws InterruptedException
{
This process does not make any sense so to overcome with this we will move all l
ocator in a separate file and we will link all test cases to this file. In case
any changes happen in our locator we will simply change in that file, not our te
st cases.
This will increase the modularity of test cases and I will strongly suggest that
you should use Object Repository in Automation.
Object Repository in Selenium Webdriver- Selenium Tutorial
Precondition
1- Project should be created and Selenium jars should be added- Click here to co
nfigure .
2- Now create a new file and give file name as Object_Repo (depends on you)with
extension .properties
For this right click on project > create a new file > Specify name
3- Now file is created > Double click on file > File will open in Edit mode
Object repository works on KEY and Value pair so we will specify Keys based on o
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.firefox.FirefoxDriver;
org.testng.annotations.Test;
}
public static WebElement btn_LogIn(WebDriver driver){
element = driver.findElement(By.id("login"));
return element;
}
}
Driver is being passed as an Argument so that Selenium is able to locate the ele
ment on the browser (driver).
Element is returned, so that an Action can be performed on it.
Method is declared as Public Static, so that it can be called in any other metho
d without instantiate the class.
Follow the same rule for creating LogIn Page class.
package pageObjects;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LogIn_Page {
private static WebElement element = null;
public static WebElement txtbx_UserName(WebDriver driver){
element = driver.findElement(By.id("log"));
return element;
}
public static WebElement txtbx_Password(WebDriver driver){
element = driver.findElement(By.id("pwd"));
return element;
}
public static WebElement btn_LogIn(WebDriver driver){
element = driver.findElement(By.id("login"));
return element;
}
}
4) Create a
New Class
automationFramewo
rk Package and select New > Class. We will be creating all our test cases under t
his package.
Now convert your old First Test Case in to the new Page Object Model test case.
package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
// Import package pageObject.*
import pageObjects.Home_Page;
import pageObjects.LogIn_Page;
public class PageObjectModel {
private static WebDriver driver = null;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.store.demoqa.com");
// Use page Object library now
Home_Page.lnk_MyAccount(driver).click();
LogIn_Page.txtbx_UserName(driver).sendKeys("testuser_1");
LogIn_Page.txtbx_Password(driver).sendKeys("Test@123");
LogIn_Page.btn_LogIn(driver).click();
System.out.println(" Login Successfully, now it is the time to Log Off buddy.")
Home_Page.lnk_LogOut(driver).click();
driver.quit();
}
}
You will notice that once you type Home_Page in your test script and the moment
you press dot, all the methods in the Home Page will display. We can expose meth
ods in order to reduce duplicated code. We are able to call these method multipl
e times. This will ensure a better maintainable test code, because we only have
to make adjustments and improvements in one particular place.