Static Block static block (also called static clause) which can be used for static initializations of a class. This code inside…
Properties File Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
//To create own page object repository. file_name : config.properties #########@ Global properties @######## browser= chrome url= http://parabank.parasoft.com username=john password=demo ###########Login object############# user_name = username password_name = password Login_btn =//input[@type='submit'] ################################### //write a code using properties file to login para_bank application. package com.SeleniumTest; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; 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; import org.openqa.selenium.safari.SafariDriver; // Read properties File: public class ReadClass { public static void main(String[] args) throws IOException { WebDriver driver = null; //step1: how to read properties file: Properties prop = new Properties(); //step2 :create file I/p class: and load file path. FileInputStream ip = new FileInputStream("E:/NewJavaProject/SeleniumPractice/SeleniumPractice/config.properties"); //step3: load this file I/p class reference loaded. prop.load(ip); //step4: access browser.(establish connection) System.out.println(prop.getProperty("browser")); String browserName = prop.getProperty("browser"); if(browserName.equals("chrome")){ System.setProperty("webdriver.chrome.driver","C:\\selenium\\chromedriver.exe"); driver = new ChromeDriver(); } else if(browserName.equals("FF")){ driver = new FirefoxDriver(); } else if(browserName.equals("safari")){ driver = new SafariDriver(); } else if(browserName.equals("IE")){ driver = new InternetExplorerDriver(); } else { System.out.println("no browser value is given"); } driver.get(prop.getProperty("url")); driver.findElement(By.name(prop.getProperty("user_name"))).sendKeys(prop.getProperty("username")); driver.findElement(By.name(prop.getProperty("password_name"))).sendKeys(prop.getProperty("password")); driver.findElement(By.xpath("//input[@type='submit']")).click(); } } |
Read data using excel file data driven approach using TestNG (Data Provider). Reading Login details from External Excel file using…
Types of Java Constructor and its usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
/** * */ package defaultConstructorCallAfterCreatingMultiOBJ; /** * @author Kushal Thadani * */ public class DefaultConstructor { int id; int ids; int count; String emp_name ; String designation; float salary; DefaultConstructor(){ ids =1012; } DefaultConstructor(int id){ this.id =1; } DefaultConstructor(int count , String emp_name){ this.id =10; this.emp_name = "Kushal Thadani"; } DefaultConstructor(int count , String emp_name , float salary){ this.id =10; this.emp_name = "Kushal Thadani"; this.salary = 1200000.00f; } DefaultConstructor(int count, String emp_name ,float salary , String designation){ this.count = count; this.emp_name =emp_name; this.salary = salary; this.designation = designation; } void m() { System.out.println(ids); } void m1() { System.out.println(id + "\t" +emp_name + "\t" + salary); } void m3() { System.out.println(count + "\t" + emp_name + "\t" + salary + "\t" +designation); } public static void main(String[] args) { DefaultConstructor t = new DefaultConstructor(); DefaultConstructor t1 = new DefaultConstructor(10 , "Parameter Constructor" , 1200000.00f); DefaultConstructor t2 = new DefaultConstructor(112 , "Kushal Thadani" , 102300.54f , "Developer"); DefaultConstructor t3 = new DefaultConstructor(1123 , "Rahul Shah" , 50400.90f , "Designer"); t.m(); t1.m1(); t2.m3(); t3.m3(); } } |
Test Case File:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class TC_Login{ @Test public void t_01_validate_login() { WebDriver driver = WebDriverManager.InitializeBrowser("chrome", "http://parabank.parasoft.com/parabank/index.htm"); String expected_url = "http://parabank.parasoft.com/parabank/index.htm"; //Create Page Factory object for Login page PO_Login PO_Login = PageFactory.initElements(driver, PO_Login.class); PO_AccountOverview PO_AccountOverview = PageFactory.initElements(driver, PO_AccountOverview.class); PO_Login.SetUserNameTextBox("john"); PO_Login.SetPasswordTextBox("demo"); PO_Login.ClickLoginButton(); //Create Page Factory object for Account Overview PO_AccountOverview.ValidateAccountOverviewTableIsDisplayed(); /* List<WebElement> collection = driver.findElements(By.xpath("//input[@type='text']")); int total = collection.size(); for(int i=0;i<total;i++) { collection.get(i).clear(); } */ }//end method |
Page Object Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
package po; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.How; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import reusables.GenericReusables; public class PO_Login { //Step1 : Define the driver WebDriver driver; //Step 2: Paramatrize the constructor public PO_Login(WebDriver driver1) { this.driver = driver1; } //Step 3: Declare the Locators // Two ways to declare Locators @FindBy(how=How.NAME,using="username") WebElement txtbx_username; @FindBy(how=How.NAME,using="password") WebElement txtbx_password; @FindBy(how=How.XPATH,using="//input[@type='submit']") WebElement btn_login; /* @FindBy(id = "dsf") WebElement sdfds; */ //Set user name public void SetUserNameTextBox(String username) { try { WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOf(txtbx_username)); txtbx_username.sendKeys(username); GenericReusables.WriteLogs("info", "User Name set with text: " + username); }catch(NoSuchElementException e) { GenericReusables.WriteLogs("fail", "User Name not set with text: " + username + " due to exception " + e.toString()); e.printStackTrace(); } } //Set user name public void SetPasswordTextBox(String password) { try { WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOf(txtbx_password)); txtbx_password.sendKeys(password); GenericReusables.WriteLogs("info", "Password set with text: " + txtbx_password); }catch(NoSuchElementException e) { GenericReusables.WriteLogs("fail", "Password not set with text: " + password + " due to exception " + e.toString()); e.printStackTrace(); } }//end Method //Set user name public void ClickLoginButton() { try { WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOf(txtbx_password)); btn_login.click(); GenericReusables.WriteLogs("info", "Clicked on Login Button"); }catch(NoSuchElementException e) { GenericReusables.WriteLogs("fail", "Unable to click on login button" + " due to exception " + e.toString()); e.printStackTrace(); } }//end Method }//end class |
Need to Automate below Scenarios: * Test cases in this Pack. * TC 1: Validate account table is displayed *…
This code is a Sample code to describe how tests can be created using Junit and best way to structure…
Handling of Frame and Frame-Set Selenium Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
WebDriver driver = new ChromeDriver(); driver.get("file:///C:/Automation/FrameProblem/FrameProblem.html"); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //Switching to 2nd Parent Frame driver.switchTo().frame(1); //Switching to frame inside frameset driver.switchTo().frame(0); //or //WebElement o_frameset = driver.findElement(By.tagName("frameset")); //WebElement o_frameset_frame = o_frameset.findElement(By.tagName("frame")); //driver.switchTo().frame(o_frameset_frame); WebElement obj = driver.findElement(By.tagName("input")); obj.sendKeys("Akash"); |
HTML which Above Code Handles:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<!DOCTYPE html> <html> <body> <h1>The Cascaded Frames Problem</h1> <div> <iframe src="./Frame1.html"> <!DOCTYPE html> <html> <body> <h3>Frame 1</h3> </body> </html> </iframe> <iframe src="./Frame2.html"> <!DOCTYPE html> <html> <frameset cols="25%,50%,25%"> <frame src ="./frame3.html"> <html> <body> <input type="text"></input> </body> </html> </frame> </frameset> </html> </iframe> </div> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
package sampletests; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class MyFirstTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:\\VisionITWorkspace\\dependencies\\chromedriver_win32\\chromedriver.exe"); //1 Create Driver object WebDriver driver = new ChromeDriver(); //Navigate driver.get("http://parabank.parasoft.com"); //Locators for Login WebElement txt_username = driver.findElement(By.xpath("//input[@name='username' and @class='input']")); WebElement txt_password = driver.findElement(By.xpath("//input[@name='password' and @class='input']")); WebElement btn_submit = driver.findElement(By.xpath("//input[@value='Log In' and @type='submit']")); //Operation on- Login in to application txt_username.sendKeys("john"); txt_password.sendKeys("demo"); btn_submit.click(); //Click on update contact info WebElement link_update_contact_info = driver.findElement(By.linkText("Update Contact Info")); link_update_contact_info.click(); //Locators for all the fields from Contact Info //WebElement txtbx_firstname = driver.findElement(By.id("customer.firstName")); // WebElement txtbx_lastname = driver.findElement(By.id("customer.lastName")); //WebElement txtbx_add_street = driver.findElement(By.id("customer.address.street")); //WebElement txtbx_add_city = driver.findElement(By.id("customer.address.city")); //WebElement txtbx_add_state = driver.findElement(By.id("customer.address.state")); //WebElement txtbx_add_zip_code = driver.findElement(By.id("customer.address.zipCode")); //WebElement txtbx_phn_number = driver.findElement(By.id("customer.phoneNumber")); //System.out.println(txtbx_firstname.getAttribute("value")); //Find Elements List<WebElement> txtbx_collection = driver.findElements(By.xpath("//input[@type='text']")); String[] arr_input = {"Akash","Tyagi","add1","add2","add3","add4","976983939"}; for(int i=0;i<txtbx_collection.size();i++) { txtbx_collection.get(i).clear(); txtbx_collection.get(i).sendKeys(arr_input[i]); } WebElement sendpayment = driver.findElement(By.xpath("//input[@value='Update Profile']")); /* txtbx_collection.get(0).sendKeys("Akash"); txtbx_collection.get(1).sendKeys("Tyagi"); txtbx_collection.get(2).sendKeys("add1"); */ } } |