This is a demonstration on how to control test cases using test ng xml driver file. There are 4 classes;Ecommerce,…
We can use these annotations in those cases when we have more than a single criteria to to identify one…
Scrum Call 2: Scrum Call Pattern, Atlassian Jira, Sprint Planning, Estimate, Story Points, Test Cases
Scrum Call Pattern, Atlassian Jira, Sprint Planning, Estimate, Story Points
CS cart Basic Automation Script Using TestNG
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
package com.visionit.tc; import org.testng.annotations.Test; import org.testng.asserts.Assertion; import org.testng.annotations.BeforeMethod; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; public class SmokeTests { @Test public void TC_01_InvokeCSCartURLAndValidate() { System.setProperty("webdriver.chrome.driver", "E:\\_AkashStuff\\Automation\\dependencies\\chromedriver\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.manage().deleteAllCookies(); driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); driver.get("https://demo.cs-cart.com/stores/78f9102f62336286/"); String s_expected_title = "Shopping Cart Software & Ecommerce Software Solutions by CS-Cart"; String s_actual_title = driver.getTitle(); Assert.assertEquals(s_expected_title, s_actual_title); //FInd elements WebElement txtbx_searchInput = driver.findElement(By.id("search_input")); boolean b_expected_searchbox = true; boolean b_actual_serarchbox = txtbx_searchInput.isDisplayed(); Assert.assertEquals(b_expected_searchbox, b_actual_serarchbox); } @Test public void TC_02_ValidateSearchFuntionality() { //************************************************** //**********************Pre************************* //************************************************** WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.manage().deleteAllCookies(); driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); driver.get("https://demo.cs-cart.com/stores/78f9102f62336286/"); String s_expected_title = "Shopping Cart Software & Ecommerce Software Solutions by CS-Cart"; String s_actual_title = driver.getTitle(); Assert.assertEquals(s_expected_title, s_actual_title); //FInd elements WebElement txtbx_searchInput = driver.findElement(By.id("search_input")); boolean b_expected_searchbox = true; boolean b_actual_serarchbox = txtbx_searchInput.isDisplayed(); Assert.assertEquals(b_expected_searchbox, b_actual_serarchbox); //************************************************** //***************Actual Test Steps****************** //************************************************** //Enter Computer in Search Box txtbx_searchInput.sendKeys("Computer"); //Click on Submit driver.findElement(By.xpath("//button[@class = 'ty-search-magnifier']")).click(); WebElement panel_product_search = driver.findElement(By.id("products_search_11")); //Checkpoint 1: for Product Search panel Assert.assertEquals(panel_product_search.isDisplayed(), true); //Checkpoint 2: Validate and Print Products displayed List<WebElement> list_products = driver.findElements(By.xpath("//a[@class='product-title'")); for (int i = 0;i<=list_products.size();i++) { String p = list_products.get(i).getText(); if ((p.contains("ASUS")) || (p.contains("CASIO")) ) { Assert.assertTrue(true); // break; }else { Assert.assertTrue(false); } //Assert.assertTrue(p.contains("ASUS")); }//end for }//end class @BeforeMethod public void beforeMethod() { } @AfterMethod public void afterMethod() { } } |
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 |
@Override public HashMap<Integer, HashMap<Integer, String>> GetUITableText(WebElement _locator) { // TODO Auto-generated method stub List<WebElement> o_col_rows = _locator.findElements(By.tagName("tr")); int i_row_count = o_col_rows.size(); List<WebElement> o_col_clms; int i_clm_count; String cell_text; //HashMap<Integer,HashMap<Integer,String>> o = new HashMap<Integer, HashMap<Integer, String>> result_map= new HashMap<Integer,HashMap<Integer,String>>(); HashMap<Integer, String> o_col_map=null;; for (int i=0;i<i_row_count;i++) { o_col_clms = o_col_rows.get(i).findElements(By.tagName("td")); i_clm_count = o_col_clms.size(); for(int j=0;j<i_clm_count;j++) { cell_text = o_col_clms.get(j).getText(); //System.out.println(cell_text); o_col_map.put(j, cell_text); } result_map.put(i, o_col_map); } return result_map; }//end method |
What is A FW A FW is a blueprint or a set of guidelines to organize the code. It tells…
Common Interview Questions and Sample Answers Created: Akash On:9Sep2018 Revision History: Tell me about yourself. My Name is XYZ. I…
PO_CmnPageObjects.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package pageobjects; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.How; public class PO_CmnPageObjects { WebDriver driver; public PO_CmnPageObjects(WebDriver driver) { this.driver = driver; } @FindBy(how = How.LINK_TEXT, using = "Transfer Funds") private WebElement link_transfer_funds; public void ClickTransferFunds() { link_transfer_funds.click(); } } |
PO_Login.java
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 |
package pageobjects; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.How; public class PO_Login { WebDriver driver; public PO_Login(WebDriver driver) { this.driver = driver; } @FindBy(how = How.NAME, using = "username") private WebElement txtbx_login; @FindBy(how = How.NAME, using = "password") private WebElement txtbx_password; @FindBy(how = How.XPATH, using = "//input[@type='submit' and @value='Log In']") private WebElement btn_login; public void SetUserName(String u) { txtbx_login.sendKeys(u); } public void SetPassword(String p) { txtbx_password.sendKeys(p); } public void ClickLoginBtn() { btn_login.click(); } } |
PO_TransferFunds.java
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 |
package pageobjects; import org.openqa.selenium.By; 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.Select; import junit.framework.Assert; public class PO_TransferFunds { WebDriver driver; public PO_TransferFunds(WebDriver driver) { this.driver = driver; } @FindBy(how = How.ID, using = "amount") private WebElement txtbox_ammount; @FindBy(how = How.ID, using = "fromAccountId") private WebElement list_from_account; @FindBy(how = How.ID, using = "toAccountId") private WebElement list_to_account; @FindBy(how = How.XPATH, using = "//input[@value = 'Transfer']") private WebElement btn_tranfer; @FindBy(how = How.XPATH, using = "//div[@ng-if = 'showResult']") private WebElement txt_container_transfer_results; public void SetAmmount(String u) { txtbox_ammount.sendKeys(u); } public void SelectFromAccount(String p) { Select oFromList = new Select(list_from_account); oFromList.selectByValue(p); } public void SelectToAccount(String p) { Select oToList = new Select(list_to_account); oToList.selectByValue(p); } public void ClickTransfer() { btn_tranfer.click(); } /* * Transfer Complete! $123.00 has been transferred from account #13122 to account #13566. See Account Activity for more details. */ public void ValidateTransferResults() { //Validation 1 boolean result_actual = txt_container_transfer_results.isDisplayed(); boolean result_expected = true; Assert.assertEquals(result_actual, result_expected); //Validation 2 String result_text_actual = txt_container_transfer_results.getText(); String result_text_expected_1 = "Transfer Complete!"; boolean b = (result_text_actual.contains(result_text_expected_1)); Assert.assertTrue(b); //String result_text_expected_2 = "has been transferred from account"; //String result_text_expected_3 = "See Account Activity for more details."; //Assert.assertEquals(result_text_actual, result_text_expected_1); //Assert.assertEquals(result_text_actual, result_text_expected_2); //Assert.assertEquals(result_text_actual, result_text_expected_3); } } |
TC_SmokeTest.java
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 testcases; import static org.junit.jupiter.api.Assertions.*; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.PageFactory; import junit.framework.Assert; import pageobjects.PO_CmnPageObjects; import pageobjects.PO_Login; import pageobjects.PO_TransferFunds; class TC_SmokeTestJunit { WebDriver driver; PO_CmnPageObjects PO_CmnPageObjects; PO_TransferFunds PO_TransferFunds; @BeforeAll static void setUpBeforeClass() throws Exception { System.out.println("BeforeAll"); System.setProperty("webdriver.chrome.driver", "D:\\VisionITWorkspace\\dependencies\\chromedriver_win32\\chromedriver.exe"); } @BeforeEach void setUp() throws Exception { //1. Initialize the Browser driver = new ChromeDriver(); driver.get("http://parabank.parasoft.com/parabank/index.htm"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); PO_CmnPageObjects = PageFactory.initElements(driver, PO_CmnPageObjects.class); PO_TransferFunds = PageFactory.initElements(driver, PO_TransferFunds.class); System.out.println("BeforeEach"); } @AfterEach void tearDown() throws Exception { driver.quit(); System.out.println("Driver quit"); } @Test void t_login_into_parabank() { System.out.println("Test1"); PO_Login o_login = PageFactory.initElements(driver, PO_Login.class); o_login.SetUserName("john"); o_login.SetPassword("demo"); o_login.ClickLoginBtn(); String s_title_expected = "ParaBank | Accounts Overview"; String s_title_actual = driver.getTitle(); Assert.assertEquals(s_title_expected, s_title_actual); //fail("Not yet implemented"); } @Test void t_check_transfer_funds() { //Login in to the application t_login_into_parabank(); //Click on Transfer funds PO_CmnPageObjects.ClickTransferFunds(); //Set Amount and perform Transfer Funds PO_TransferFunds.SetAmmount("1000"); PO_TransferFunds.SelectFromAccount("12345"); PO_TransferFunds.SelectToAccount("12346"); PO_TransferFunds.ClickTransfer(); //Validation PO_TransferFunds.ValidateTransferResults(); } } |
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 |
public class AlertHandling { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver", "D:\\VisionITWorkspace\\dependencies\\chromedriver_win32\\chromedriver.exe"); //1 Create Driver object WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Navigate and click on the link driver.get("https://www.w3schools.com/jsref/met_win_alert.asp"); WebElement lnk_try_it_urself = driver.findElement(By.partialLinkText("Try it Yourself")); //this link will open new tab because of its implementation of <a target= "_blank"> lnk_try_it_urself.click(); //Switch to New tab //Get windows handles ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles()); //Two handle will be returned System.out.println(tabs.size()); //Switch to 2nd tab driver.switchTo().window(tabs.get(1)); //Switch to Frame because Choose from File is present in the frame WebElement frame = driver.findElement(By.id("iframeResult")); driver.switchTo().frame(frame); //Click on Try it WebElement btn_try_it = driver.findElement(By.xpath("//button[contains(text(),'Try it')]")); btn_try_it.click(); //TO slow down the Execution Thread.sleep(5000); //To click on Ok driver.switchTo().alert().accept(); //USe Dismiss to Click on cancel button //https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_confirm driver.switchTo().alert().dismiss(); // to cancel driver.switchTo().alert().sendKeys("Akash"); // to send keys } } |
Video: Document to Install and Write Script for Auto IT: AutoItDocument Code to Call Auto It Script from Java:…
Video Link:
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 |
/* * Author: Akash Tyagi * Date: 22Sep2018 */ public class MultiWindow { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "D:\\VisionITWorkspace\\dependencies\\chromedriver_win32\\chromedriver.exe"); //1 Create Driver object WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Navigate and click on the link driver.get("https://www.w3schools.com/jsref/met_win_alert.asp"); WebElement lnk_try_it_urself = driver.findElement(By.partialLinkText("Try it Yourself")); //this link will open new tab because of its implementation of <a target= "_blank"> lnk_try_it_urself.click(); //Get windows handles ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles()); //Two handle will be returned System.out.println(tabs.size()); //Switch to 2nd tab driver.switchTo().window(tabs.get(1)); //perform a click operation on the 2nd tab to check the switch is completed WebElement btn_save = driver.findElement(By.xpath("//a[@title = 'Save']")); btn_save.click(); Thread.sleep(5000); //This will close only the current tab. //This is a basic difference between driver.quit and driver.close //driver close will only close the current window or tab //where as driver quit will quit all the windows open under the driver driver.close(); //Return to default window driver.switchTo().window(tabs.get(0)); //driver quit //driver.quit(); //will close all the windows } } |
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"); */ } } |
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
/* * Author: Akash Tyagi * Date:28Dec2017 * Detail: To explain use of Interface and Inheritance * in developing Core Selenium FW component in java for UI Operations * */ package _test; import org.openqa.selenium.WebElement; //Method List for all the Sync operations interface IUISync { //Default Method default public boolean waitForElementToAppear(WebElement element, int TimeOut){ return true; } //Default Method default public boolean waitForElementAttributeToAppear(WebElement element, String AttName, String ExpAttValue, int TimeOut){ return true; } //Default Method default public boolean waitForPageToLoad(int TimeOut){ return true; } //Default Method default public boolean waitForPageUrlToChange(String old_url, int TimeOut){ return true; } }//end Interface interface IUIEventsGeneric extends IUISync { //Default Methods in Generic UI events Interface default public String getText(WebElement element){ return element.getText(); } default public String getAttribute(WebElement element, String att_name){ return element.getAttribute(att_name); } }//end interface //Method List for all the Link Specific Operations interface IUIEventsLink { public void click(); }//end interface //Method List for all the Text Box Specific Operations interface IUIEventsTextBox{ public void sendKeys(String text); }//end interface //Method List for all the table specific operations interface IUIEventsTable { public String getTableContent(); public int getRowWithCellText(String s_text); }//end interface //UIMethods Class implements all the UI methods class UIMethods implements IUIEventsGeneric,IUIEventsLink,IUIEventsTextBox,IUIEventsTable,IUISync{ WebElement element; public UIMethods(WebElement obj){ this.element = obj; } public void click(){ waitForElementToAppear(element,10); element.click(); } public void sendKeys(String text){ element.sendKeys(text); } public String getTableContent(){ return getText(element); // Direct use of Default method from IUIEventsGeneric } public int getRowWithCellText(String s_text){ return 0; } }//End Class |
1 2 3 4 5 6 7 8 |
int a = -9; a = -a; System.out.println(a); //This will print 9 int b = 9; b = -b; System.out.println(b); //This will print -9 |
Check this Link for More: StackOverFlowQuestion
To Demonstrate use of Interfaces and Inheritance in Selenium FW
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
/* * Author:Akash Tyagi * Detail: To Demonstrate Use of Interfaces and Inheritance in Selenium FW * */ namespace selenium_root { //Interface with all the Possible Events interface IUIEvents { void click(IWebElement obj); void sendKeys(IWebElement obj, string textToBeSet); void selectList(IWebElement obj); string getText(IWebElement obj); void getAttributeValue(IWebElement obj); void rightMouseClick(IWebElement obj, int iX, int iY); void moveMouse(IWebElement obj, int iX, int iY); void dragAndDrop(IWebElement obj, int iX, int iY); } //UIEvents Class contains all the all the event based methods //Implements Interface: IUIEvents class UIEvents : IUIEvents { public void click(IWebElement obj) { /*Definition Yet to be Written*/} public void sendKeys(IWebElement obj, string textToBeSet) { /*Definition Yet to be Written*/} public void selectList(IWebElement obj) { /*Definition Yet to be Written*/} public string getText(IWebElement obj) { /*Definition Yet to be Written*/ return ""; } public void getAttributeValue(IWebElement obj) { /*Definition Yet to be Written*/} public void rightMouseClick(IWebElement obj, int iX, int iY) { /*Definition Yet to be Written*/} public void moveMouse(IWebElement obj, int iX, int iY) { /*Definition Yet to be Written*/} public void dragAndDrop(IWebElement obj, int iX, int iY) { /*Definition Yet to be Written*/} } //Nested Class with Parent Class as UIControls containing all the Control Based Methods //Control Based methods implements UIEvents Classes class UIControls { class UITable : UIEvents { public string getTableText(IWebElement obj) { return getText(obj); } public void getTableCellTextInRowAndClm(IWebElement obj, int iRow, int iClm) { //code to written } public void getTableRowWithCellTextAs(IWebElement obj) { /*Definition Yet to be Written*/ } public List<IWebElement> getTableCellElementsInRowAndClm(IWebElement objTable, int iRow, int iClm) { return null; } } class UIList : UIEvents { public void selectListByValue(IWebElement obj, string sValue) { } public void selectListByIndex(IWebElement obj, string sValue) { //code to written } public string getListText(IWebElement obj) { return getText(obj); } } class UILink : UIEvents { public void clickLink(IWebElement obj) { click(obj); } }//end class class UITextBox : UIEvents { public void clickTextBox(IWebElement obj) { click(obj); } public void setValueInTextBox(IWebElement obj, string textToBeSet) { sendKeys(obj, textToBeSet); } }//end class } }//end name space |
To Get List names and Paths of all the Files inside a root folder. Below Code recursively traverse every folder…
Serialization is the process of converting an object’s state (including its references) to a sequence of bytes, as well as…
Data streams support binary I/O of primitive data type values (boolean, char, byte, short, int, long, float, and double) as…
Most of the examples we’ve seen so far use unbuffered I/O. This means each read or write request is handled…
Character streams are often “wrappers” for byte streams. The character stream uses the byte stream to perform the physical I/O,…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //Byte Streams FileInputStream in=null; FileOutputStream out = null; in = new FileInputStream("C:\\temp.txt"); out = new FileOutputStream("C:\\temp1.txt"); int c; while ( (c=in.read()) != -1 ){ out.write(c); } in.close(); out.close(); } |
I/O Streams Byte Streams handle I/O of raw binary data. Character Streams handle I/O of character data, automatically handling translation…
To handle multiple tabs you have to make a switch between drivers.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles()); driver.switchTo().window(tabs.get(1)); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(driver.getTitle()); o_search.o_add_to_cart_on_product_detail.click(); // driver.close(); driver.switchTo().window(tabs.get(0)); |
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 |
public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/Users/akashtyagi/Documents/SeleniumFW_SVN/dependencies/chromedriver"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.get("https://www.w3schools.com/html/html_tables.asp"); WebElement o_tbl_object = driver.findElement(By.id("customers")); LinkedHashMap<Integer,LinkedHashMap<Integer,String>> o_result = f_get_table_content(o_tbl_object); System.out.println(o_result.toString()); } public static LinkedHashMap<Integer,LinkedHashMap<Integer,String>> f_get_table_content(WebElement o_tbl_object){ try{ LinkedHashMap<Integer,String> o_clm_data;// = new LinkedHashMap<Integer,String>(); LinkedHashMap<Integer,LinkedHashMap<Integer,String>> tbl_map_result = new LinkedHashMap<Integer,LinkedHashMap<Integer,String>>(); List<WebElement> list_rows = o_tbl_object.findElements(By.tagName("tr")); List<WebElement> list_clms; int i_row_count = list_rows.size(); int i_clm_count;// = list_clms.size(); for(int i=0;i<i_row_count;i++){ list_clms = list_rows.get(i).findElements(By.tagName("td")); i_clm_count = list_clms.size(); o_clm_data = new LinkedHashMap<Integer,String>(); for(int j=0;j<i_clm_count;j++){ o_clm_data.put(j,list_clms.get(j).getText()); }//End Inner For tbl_map_result.put(i, o_clm_data); }//End Outer For if( (tbl_map_result.get(0).size()>0) && (tbl_map_result.size()>0) ){ System.out.println("info:HTML table fetched and returned in dual hashmap"); }else{ System.out.println("fail:Unable to fetch data from table. Row Count:" + (tbl_map_result.size()) + " Clm Count: " + (tbl_map_result.get(0).size()) ); } return tbl_map_result; }catch(Exception e){ System.out.println("fail:Unable to fetch data from Html table." + " Stack Trace: " +e.getMessage()); return null; } }//End Function |
1 |