Extent Report Implementation using new Extent Report TestNG ITest Listener. Check for the Latest copy of the Framework here at…
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 |
package SeleniumSessions.DatePicker; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; /** * @author Sarang * @Date - 21/05/2019 */ public class DatePickerOnGoIbibo { WebDriver driver; String URL = "https://www.goibibo.com/"; String MonthToBeSelected = "September 2020"; String DAY = "15"; @BeforeMethod public void setup() { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().deleteAllCookies(); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); } @Test public void datePicker() throws InterruptedException { // passing URL driver.get(URL); // Clicking on Departure Date Picker Box driver.findElement(By.xpath("//input[@type='text' and @placeholder='Departure']")).click(); // Logic for Expected Date Picker while (true) { String monthOnPage = driver.findElement(By.xpath("//div[@class='DayPicker-Caption' and @role='heading']")).getText(); if (monthOnPage.equals(MonthToBeSelected)) { break; } else { driver.findElement( By.xpath("//span[@role='button' and @class='DayPicker-NavButton DayPicker-NavButton--next']")).click(); } } // Clicking over the DAY variable date driver.findElement(By.xpath("//div[@class='DayPicker-Week']/div[@class='DayPicker-Day']/div[text()=" + DAY + "]")).click(); } @AfterMethod public void teardown() { driver.close(); } } |
This is a many part videos to create a working Selenium test automation framework to automate a retail banking application.…
Navigate to URL: https://ca.ingrammicro.com Search for product: Apple Assert that search result is displayed. Create a method which takes arrays of…
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 |
public String TakeScreenShot(WebDriver driver) { try { //Get the random number for the file Random rand = new Random(); long random = (int )(Math.random() * 999999999 + 1000000); String result = Long.toString(random); String path = System.getProperty("user.dir") + "//" +"ScreenShots"; String nameFileName = result + ".png"; String filePathName = path + "//" + nameFileName; //to create new folder new File(path).mkdirs(); // TODO Auto-generated method stub TakesScreenshot scrShot = (TakesScreenshot)driver; File srcFile = scrShot.getScreenshotAs(OutputType.FILE); File destFile = new File(filePathName); Files.copy(srcFile.toPath(), destFile.toPath()); //WriteLogAndReport(logger, "info", "info", "Screen Shot taken at location: " + path); //Reporter.log("Screen shot taken and kept at path: " + filePathName ); Reporter.log("<a href = '" +filePathName + "'>My ScreenShot link</a>"); return filePathName; }catch (Exception e) { e.printStackTrace(); return null; } } |
Below Scenario is to Validate the Search functionality of an E Commerce Web Site: URL: https://ca.ingrammicro.com/ Below Code demonstrate three…
This Scenario can be mentioned as an answer to the Question : Explain a challenging scenario/task you recently faced and…
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 |
package SeleniumSessions; 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; /** * * @author Sarang * Created on 16/04/2019 * */ public class AuthenticationURL { public static void main(String[] args) { // WebDriver Configuration System.getProperty("webdriver.chrome.driver", "chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Here inside URL USername and Password is being sent // Username - admin // Password - admin // after text http://username:password@Url is to be the syntax driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth"); // Validation Check Point WebElement successfulAuthText = driver.findElement(By.xpath("//div[@id='content']//p")); System.out.println(successfulAuthText.getText()); String ActualText ="Congratulations! You must have the proper credentials."; if(successfulAuthText.equals(ActualText)) { System.out.println("Authentication is successful"); } else { System.out.println("Authentication is Unsuccessful"); } } } |
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 |
package SeleniumSessions; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import org.testng.Assert; import org.testng.annotations.Test; /** * * @author Sarang * Created on 19/04/2019 * */ public class KeyPressEventInSelenium { static WebDriver driver; @Test public void keyPressEventSpace() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); driver = new ChromeDriver(); driver.get("http://the-internet.herokuapp.com/key_presses"); Actions action = new Actions(driver); action.sendKeys(Keys.SPACE).build().perform();; String text = driver.findElement(By.id("result")).getText(); System.out.println(text); Assert.assertEquals(text, "You entered: SPACE"); Thread.sleep(2000); driver.quit(); } @Test public void keyPressEventEnter() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); driver = new ChromeDriver(); driver.get("http://the-internet.herokuapp.com/key_presses"); Actions action = new Actions(driver); action.sendKeys(Keys.ENTER).build().perform();; String text = driver.findElement(By.id("result")).getText(); System.out.println(text); Assert.assertEquals(text, "You entered: ENTER"); Thread.sleep(2000); driver.quit(); } } |
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 |
package SeleniumSessions; import java.io.File; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; /** * * @author Sarang * Created on 20/04/2019 * */ public class DesiredCapabilitiesChrome { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","chromedriver.exe"); ChromeOptions options = new ChromeOptions(); // To disable Info bar saying - Chrome is being controlled By Automated Software options.addArguments("--disable-infobars"); // To add specific Extentions on Automated Browser controlled via selenium at fresh Instance ex - AddBlockerPlus options.addExtensions(new File("../JanSeleniumTraining/AddBlockerPlus.crx")); options.addExtensions(new File("../JanSeleniumTraining/AddBlock.crx")); // To set SSL Cerificated as True which is not available with some Websites options.setAcceptInsecureCerts(true); WebDriver driver = new ChromeDriver(options); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://www.popuptest.com/goodpopups.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 |
package SeleniumSessions; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.concurrent.TimeUnit; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; /** * * @author Sarang * Created on 16/04/2019 * */ public class PdfReaderTest { public static void main(String[] args) throws IOException, InterruptedException { // This "pdfbox" depenency will only fetch data from PDF which is // only available in Text not the image PDF and URL having ".pdf" should be available. // Readme - First add following dependency into Pom.xml //<dependency> //<groupId>org.apache.pdfbox</groupId> //<artifactId>pdfbox</artifactId> //<version>2.0.12</version> //</dependency> // WebDriver Configuration System.getProperty("webdriver.chrome.driver", "chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://rusenergyweek.com/upload/iblock/1b9/1b9cb0045fcda0e07be921ec922f5191.pdf"); String urlText = driver.getCurrentUrl(); URL url = new URL(urlText); InputStream is = url.openStream(); BufferedInputStream fileParse = new BufferedInputStream(is); PDDocument document = null; document = PDDocument.load(fileParse); String pdfContent = new PDFTextStripper().getText(document); System.out.println(pdfContent); if(pdfContent.contains("Adobe Acrobat XI Quick start guide")) { System.out.println("-----------*****-------------"); System.out.println("PDF is Successfully Validated"); System.out.println("-----------*****-------------"); } else { System.out.println("PDF has some Error"); } Thread.sleep(2000); driver.quit(); } } |
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 |
package SeleniumSessions; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import io.github.bonigarcia.wdm.WebDriverManager; /** * * @author Sarang * Created on 16/04/2019 * */ public class WebDriverManager { static WebDriver driver; public static void main(String[] args) { // Dependency // <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager --> // <dependency> // <groupId>io.github.bonigarcia</groupId> // <artifactId>webdrivermanager</artifactId> // <version>3.4.0</version> // </dependency> WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); //driver = new FirefoxDriver(); driver.get("https://www.google.com"); System.out.println(driver.getTitle()); } } |
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 |
package SeleniumSessions; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; /** * * @author Sarang * Created on 16/04/2019 * */ public class AuthenticationByURL { static WebDriver driver; public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); driver = new ChromeDriver(); driver.get("https://www.engprod-charter.net/api/pub/cnetloginedge/spectrum-redir?targetUrl=https%3A%2F%2Fwww.engprod-spectrum.net%2F%3Fdomain-redirect%3Dtrue"); } } |
Driver Class: To create standalone jar to distribute. Please note that This class will not be used for execution…
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…
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 |
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; public class HandleCertificate { public static void main(String[] args) { // DesiredCapabilities Predefined class inside Selenium DesiredCapabilities cap = new DesiredCapabilities(); // Creating its object Reference // Delibrately throwing SSL certificate as true to selenium // As selenium don't allow to open Website without valid(Unexpired) SSL Certificate cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); // Setting Up ChromeDriver System.setProperty("webdriver.chrome.driver", "..//chromedriver.exe"); // Creating a Object Reference for ChromeDriver // And Passing object reference of DesiredCapabilities to Accept SSL Certicate as True WebDriver driver = new ChromeDriver(cap); // Launching the URL without SSL Certificate driver.get("https://www.cacert.org/"); } } |
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
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 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class elementHighlighter { @Test public void elementHighlighterCode(){ //Creating reference variable of Webdriver WebDriver driver; // Setting up the properties for Chrome Driver System.setProperty("webdriver.chrome.driver", "C:\\Vision\\chromedriver.exe"); // Inserting Chromedriver to Webdriver refernece object driver = new ChromeDriver(); // Providing wait to load all the elements on page driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS); // Maximizing the browser window driver.manage().window().maximize(); // Passing the URL driver.get("http://www.facebook.com"); // Inspect element //Alloting Username field's Element locator - id to "username" veriable WebElement username= driver.findElement(By.id("email")); //Highlighting the Username Text box Field highLightElement(driver,username); //Sending the emailID to username text box username.sendKeys("123@gmail.com"); //Alloting Password field's Element locator id to "password" veriable WebElement password= driver.findElement(By.id("pass")); //Highlighting the Username Text box Field highLightElement(driver,password); //Sending the emailID to password text box password.sendKeys("123@gmail.com"); //Alloting Login_button field's Element locator id to "login_btn" veriable WebElement login_btn= driver.findElement(By.id("u_0_8")); //Highlighting the Login_button Field highLightElement(driver,login_btn); // Clicking on Login_button login_btn.click(); } // Element highlighter Method Code with two Arguments as "driver and WebElement to be highlighted" public static void highLightElement(WebDriver driver, WebElement element) { //Creating javascript reference variable JavascriptExecutor js=(JavascriptExecutor)driver; //Setting up the Style as Background Yellow and Border to be 2Pixel width in Red Colour for WebElement js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element); try { //Wait for JavaScript to be visible in slow Thread.sleep(1000); } catch (InterruptedException e) { //Catch if anyexception occure during runtime System.out.println(e.getMessage()); } //Changing the WebElement Properties as original Attributes //As Border Yellow to White Colour with 2Pixel Border Size js.executeScript("arguments[0].setAttribute('style','border: solid 2px white');", element); } } |
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 |
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.annotations.Test; public class BootStrapDropDown_Reusable { @Test public void BootStrapDropDown_Reusables() { //Creating reference variable of Webdriver WebDriver driver; // Setting up the properties for Chrome Driver System.setProperty("webdriver.chrome.driver", "C:\\Vision\\chromedriver.exe"); // Inserting Chromedriver to Webdriver refernece object driver = new ChromeDriver(); // Maximizing the browser window driver.manage().window().maximize(); // Passing the URL driver.get("https://www.jquery-az.com/boots/demo.php?ex=63.0_2"); // Providing wait to load all the elements on page driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Clicking on Bootstrap Dropdown driver.findElement(By.xpath("//button[contains(@class,'multiselect')]")).click(); // Get the all WebElements inside the dropdown in List List<WebElement> dropdown_list = driver.findElements(By.xpath("//ul[contains(@class,'multiselect-container dropdown-menu')]//li//a//label")); // Printing the amount of WebElements inside the list System.out.println("The Options in the Dropdown are: " + dropdown_list.size()); // Condition to get the WebElement for list and Click over "Angular" option for(int i=0; i<dropdown_list.size(); i++) { // Printing All the options from the dropdown System.out.println(dropdown_list.get(i).getText()); // Checking the condition whether option in text "Angular" is comming if(dropdown_list.get(i).getText().contains("Angular")) { // Clicking if text "Angular" is there dropdown_list.get(i).click(); // Breaking the condition if the condition get satisfied break; } } } } |
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 |
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.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.Test; public class Ajaxdemo { static WebDriver driver; //Creating reference variable of Webdriver static WebDriverWait wait; //Creating WebDriverWait reference variable @Test public void ajax_logic_try() //Try to click on WebElement which is dynamic on webpage { // Setting up the properties for Chrome Driver System.setProperty("webdriver.chrome.driver", "C:\\Vision\\chromedriver.exe"); // Inserting Chromedriver to Webdriver refernece object driver = new ChromeDriver(); // Maximizing the browser window driver.manage().window().maximize(); // Passing the URL driver.get("https://www.moneycontrol.com/"); // Providing wait to load all the elements on page driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // logic try { // Inserting WebdriverWait with "driver" reference and timeout for 5 Seconds wait = new WebDriverWait(driver, 5); // Saving WebElement having hyperlinking in crudeOil_text vaeriable WebElement crudeOil_text = driver.findElement(By.linkText("CRUDEOIL")); // wait until WebElement having hyperlinking in crudeOil_text is visible on webpage wait.until(ExpectedConditions.visibilityOf(crudeOil_text)); // clicking on WebElement crudeOil_text.click(); // Getting value of WebElement inside Get_Text_Ajax String Get_Text_Ajax = crudeOil_text.getText(); // Inserting "CRUDEOIL" as text inside Exp_Text_Ajax String Exp_Text_Ajax = "CRUDEOIL"; // Inserting the text inside WebElement inside Act_text_Ajax String Act_text_Ajax = Get_Text_Ajax; // Performing Hard Assertion with text on Actual WebElement and Expected Text to be on WebElement Assert.assertEquals(Act_text_Ajax, Exp_Text_Ajax, "The actual WebElement 'CRUDEOIL' is not Present On page"); // Printing the Actual Text from the WebElement from the webpage System.out.println("The Text On Link is " + Act_text_Ajax); // Quitting the driver driver.quit(); } catch (Exception NoSuchElementException) // Catching if there is 'NoSuchElementException' situation { // Error message if Exception Arise System.out.println("Try to execute code once Again NoSuchElementException is ocurred"); } } } |
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; 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; public class BrokenLinks { public static void main(String[] args) throws InterruptedException, IOException { // //Maximize the browser System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); //Implicit wait for 10 seconds driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.facebook.com/"); //Wait for 5 seconds Thread.sleep(5000); //Used tagName method to collect the list of items with tagName "a" //findElements - to find all the elements with in the current page. It returns a list of all webelements or an empty list if nothing matches List<WebElement> links = driver.findElements(By.tagName("a")); links.addAll(driver.findElements(By.tagName("img"))); //To print the total number of links System.out.println("Total links are "+links.size()); List<WebElement>activelink=new ArrayList<WebElement>(); //used for loop to ; for(int i=0; i<links.size(); i++) { if(links.get(i).getAttribute("href")!=null) { activelink.add(links.get(i)); }} System.out.println("Active links are "+activelink.size()); System.out.println("Broken links are "+(links.size()-activelink.size())); for(int j=0;j<activelink.size();j++) { HttpURLConnection Conn =(HttpURLConnection)new URL(activelink.get(j).getAttribute("href")). openConnection(); Conn.connect(); String response=Conn.getResponseMessage();//ok Conn.disconnect(); System.out.println(activelink.get(j).getAttribute("href")+"--->"+response); } driver.quit(); } } |
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 } } |