import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class FirefoxchromeIEbrowser {
static WebDriver fireFoxDriver = null;
static WebDriver chromeDriver = null;
static WebDriver internetExplorerDriver = null;
public static void main(String[] arg) {
//create firefox instance
fireFoxDriver = new FirefoxDriver();
//Maximize browser window
fireFoxDriver.manage().window().maximize();
//Go to URL
fireFoxDriver.get("http://www.yahoo.com");
//close FireFox browser
fireFoxDriver.quit();
////set chrome driver path
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
//create chrome instance
chromeDriver = new ChromeDriver();
//Maximize browser window
chromeDriver.manage().window().maximize();
//Go to URL
chromeDriver.get("http://www.gmail.com");
//close Chome browser
chromeDriver.quit();
////set IE driver path
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir") + "\\IEDriverServer.exe");
System.out.println(System.getProperty("user.dir") + "\\IEDriverServer.exe");
//create IE instance
internetExplorerDriver = new InternetExplorerDriver();
//Maximize browser window
internetExplorerDriver.manage().window().maximize();
//Go to URL
internetExplorerDriver.get("http://www.google.com/");
//close IE browser
internetExplorerDriver.quit();
}
}
How to run script on firefox, chrome and IE parallaly on Selenium Webdriver using java?.
How to handle dynamic web tables/HTML tables in Selenium webdriver using java?.
<!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 default. 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>
<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 default. 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();
}
}
How to connect mysql database in java using jdbc?.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCconnectionExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/users";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM users";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while (rs.next()) {
//Retrieve by column name
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
//Display values
System.out.print("ID: " + id);
System.out.print(", Username: " + username);
System.out.println(", Password: " + password);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
} finally {
//finally block used to close resources
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}// nothing we can do
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end FirstExample
How to solve math captcha in selenium webdriver using java?.
public class Mathcaptcha {
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("http://localhost/mathquestion/signin");
// Type username
WebElement username = driver.findElement(By.id("edit-name"));
username.clear();
username.sendKeys("username");
//Type Password
WebElement password = driver.findElement(By.id("edit-pass"));
password.clear();
password.sendKeys("aaaaaa");
// get Math question
String mathquestionvalue = driver.findElement(By.xpath(".//*[@id='user-login']/div/fieldset/div/div[2]/span")).getText().trim();
// remove space if exist
String removespace = mathquestionvalue.replaceAll("\\s+", "");
// get two numbers
String[] parts = removespace.split("\\+");
String part1 = parts[0];
String part2 = parts[1];
String[] parts1 = part2.split("\\=");
String part11 = parts1[0];
// sum two numbers
int summation = Integer.parseInt(part1) + Integer.parseInt(part11);
// Math Capcha value
WebElement capta = driver.findElement(By.xpath(".//*[@id='edit-captcha-response']"));
capta.clear();
capta.sendKeys("" + summation);
//Click on SignIn button
driver.findElement(By.id("edit-submit")).click();
//close browser
driver.quit();
}
}
Subscribe to:
Posts (Atom)