Which concepts in Java are the most important to know while learning Selenium?

If you are a manual tester, who wants to learn Selenium (and Java) and does not know anything about programming,
these are your FIRST 100 THINGS TO LEARN.
Please consider what follows a checklist aimed at guiding your learning, nothing more.
————————————————————————-
JAVA
————————————————————————-
  1. install Java JDK
  2. install Eclipse
  3. create a simple standalone project in Eclipse that displays HELLO WORLD!
  1. public class TestClass {
  2. public static void main(String[] args) {
  3. System.out.println("HELLO WORLD!");
  4. }
  5. }
4. learn more about Eclipse: views, perspectives, debugging code
5. create variables with int, String, char, boolean, double types
  1. int number;
  2. double price;
  3. boolean isValid;
  4. char character;
  5. String text;
6. assign values to variables
  1. number = 10;
  2. price = 45.22;
  3. isValid = false;
  4. character = 'a';
  5. text = "this is a sample text";

7. use numeric operators (+, -, *, /)
  1. int number1 = 10, number2 = 20, number3 = 0, number4 = 0, number5= 0;
  2. number3 = number1 + number2;
  3. number3 = number1 * number2;
  4. number4 = number2 - number1;
  5. number5 = number2 / number1;
8. create conditions using conditional (>, >=, <, <=, ==) and logical operators (and, or, not)
  1. int number1 = 10;
  2. boolean isPositive = (number1 > 0);
  3. boolean isNegative = (number1 < 0);
  4. boolean isZero = (number1 == 0);
  5.  
  6. boolean isPositiveAndGreaterThan5 = (number1 > 0) && (number1 > 5);
  7. boolean isBetween5And15 = (number1 >=5) && (number1 <= 15);
9. use if/else to execute code based on results of conditions
  1. int number1 = 10;
  2. if (number1 < 0)
  3. System.out.println("number1 is negative.");
  4. else
  5. if (number1 == 0)
  6. System.out.println("number1 is 0.");
  7. else
  8. System.out.println("number1 is positive/");
10. use switch to execute code based on the values of a variable
  1. int number1 = 10;
  2. switch(number1) {
  3. case 1:
  4. System.out.println("number is one.");
  5. break;
  6. case 2:
  7. System.out.println("number is two.");
  8. break;
  9. case 3:
  10. System.out.println("number is three.");
  11. break;
  12. default:
  13. System.out.println("number is greater than 3.");
  14. break;
  15. }
11. use String methods
  1. String text = "this is a sample text";
  2. int length = text.length();
  3. int position = text.indexOf("is");
  4. String text1 = text.substring(5);
12. use classes from the Java API; import the packages of these classes using the import statement
  1. import java.io.File;
  2.  
  3. public class Class1 {
  4. public static void main(String[] args) {
  5.  
  6. File file = new File("c:\\temp\\files\\file1.txt");
  7. boolean fileExists = file.exists();
  8. if (fileExists == false)
  9. System.out.println("the file does not exist");
  10. long fileSize = file.length();
  11. System.out.println("\nfile length = " + fileSize);
  12.  
  13. String fileParent = file.getParent();
  14. System.out.println("file parent = " + fileParent);
  15.  
  16. boolean isFileHidden = file.isHidden();
  17. System.out.println("is file hidden? " + isFileHidden);
  18. }
  19. }
13. create a package for your class
  1. package com.java;
  2. public class TestClass {
  3. public static void main(String[] args) {
  4. System.out.println(“Hello Package!”);
  5. }
  6. }
14. use arrays
    1. initialize the array
    2. get values of array elements
    3. save values in the array elements
    4. get the array size
  1. public static void main(String[] args) {
  2. int[] numbers = new int[] {1, 4, -3, 0};
  3. System.out.println(numbers.length);
  4. numbers[2] = 5;
  5. System.out.println(numbers[2]);
  6. }
15. use lists
    1. add elements to the list
    2. remove elements from the list
    3. find an element in the list
    4. get the list size
  1. package com.java;
  2. import java.util.ArrayList;
  3. public class TestClass {
  4. public static void main(String[] args) {
  5. ArrayList<String> texts = new ArrayList<String>();
  6. System.out.println(texts.size());
  7. texts.add("aaa");
  8. texts.add("bbb");
  9. texts.add("ccc");
  10. System.out.println(texts.get(1));
  11. texts.remove(1);
  12. System.out.println(texts.size());
  13. }
  14. }
16. use Set collections
  1. Set<String> fruits = new HashSet<>();
  2. fruits.add("apple");
  3. fruits.add("watermelon");
  4. fruits.add("grape");
  5. System.out.println(fruits.contains("grape"));
17. use Map collections
  1. Map<Integer,String> products = new HashMap<Integer,String>();
  2. map.put(100,"battery");
  3. map.put(101,"pen");
  4. map.put(102,"back pack");
  5. for(Map.Entry m: products.entrySet())
  6. System.out.println(m.getKey()+" "+m.getValue());



18. use the looping statements (FOR, FOR EACH, WHILE, DO WHILE) for going through the elements of an array or list
  1. package com.java;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class TestClass {
  6.  
  7. public static void main(String[] args) {
  8. int[] numbers = new int[] {1, 3, 2, 0};
  9. for (int i = 0; i < numbers.length; i++)
  10. System.out.println(numbers[i]);
  11. ArrayList<String> texts = new ArrayList<String>();
  12. texts.add("aaa");
  13. texts.add("bbb");
  14. texts.add("ccc");
  15. for (int i = 0; i < texts.size(); i++)
  16. System.out.println(texts.get(i));
  17.  
  18. }
  19.  
  20. }
19. create a class
  1. package com.java;
  2. public class Book {
  3. private String name = "";
  4. private String author = "";
  5. public Book(String name, String author) {
  6. this.name = name;
  7. this.author = author;
  8. }
  9. public String getName() {
  10. return this.name;
  11. }
  12. public String getAuthor() {
  13. return this.author;
  14. }
  15. public void print() {
  16. System.out.println(this.name + ", " + this.author);
  17. }
  18.  
  19. }
20. create objects of a class
  1. package com.java;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class TestClass {
  6.  
  7. public static void main(String[] args) {
  8. Book b = new Book("Alice in Wonderland", "Lewis Caroll");
  9. b.print();
  10. }
  11.  
  12. }
21. work with exceptions
  • catch exceptions
  • throw exceptions
  1. int n1 = 20, n2 = 0;
  2.  
  3. try {
  4. n2 = n1 / 0;
  5. }
  6. catch (ArithmeticException e)
  7. System.out.println("cannot divide by 0.");
  8. }
  9.  
  10. if (n1 == 0)
  11. throw new RuntimeException("n1 cannot be 0.");
22. learn class inheritance
  1. public class Animal {
  2. public void move() {
  3. System.out.println("Animals can move");
  4. }
  5. }
  6.  
  7. public class Dog extends Animal {
  8. public void bark() {
  9. System.out.println("Dogs can bark");
  10. }
  11. }
  12.  
  13. public class TestDog {
  14.  
  15. public static void main(String args[]) {
  16. Dog dog = new Dog();
  17.  
  18. dog.move();
  19. dog.bark();
  20. }
  21. }
23. learn method overloading
  1. public class Sum {
  2. public int add(int a,int b){
  3. return a+b;
  4. }
  5.  
  6. public int add(int a,int b,int c){
  7. return a+b+c;
  8. }
  9. }
  10.  
  11. class TestOverloading{
  12. public static void main(String[] args){
  13. Sum sum = new Sum();
  14. System.out.println(sum.add(11,11));
  15. System.out.println(sum.add(11,11,11));
  16. }
24. method overriding
  1. public class Animal {
  2. public void move() {
  3. System.out.println("Animals can move");
  4. }
  5. }
  6.  
  7. public class Dog extends Animal {
  8. public void move() {
  9. System.out.println("Dogs can walk and run");
  10. }
  11. }
  12.  
  13. public class TestDog {
  14.  
  15. public static void main(String args[]) {
  16. Animal a = new Animal();
  17. Animal b = new Dog();
  18.  
  19. a.move();
  20. b.move();
  21. }
  22. }
25. learn how to use the access modifiers for members, methods and classes
26. abstract classes
Abstract classes are classes that cannot be instantiated (cannot create objects for them).
They should be used as parent classes for other classes.
  1. public abstract class Bike{
  2. public abstract void run();
  3. }
  4. public class Honda4 extends Bike{
  5. void run(){
  6. System.out.println("running safely..");
  7. }
  8. }
  9. public static void main(String args[]){
  10. Bike bike = new Honda4();
  11. bike.run();
  12. }
27. interfaces
  1. WebDriver driver = new FirefoxDriver();
  2. WebDriver driver = new ChromeDriver();
WebDriver is an interface implemented by both the FirefoxDriver and ChromeDriver classes.
28. use composition instead of inheritance
29. read data from text files (csv, xm, yaml, json)
30. generics
  1. List<String> list = new ArrayList<>();
31. log exceptions and errors using log4J
32. work with date and time values using the LocalDate and LocalTime classes
33. handle list of data with streams
34. reduce code duplication with predicates
35. create custom exceptions
37. read data from SQL tables
38. learn third party libraries like Apache Commons and Guava
39. learn about varargs:
  1. void foo(String... args) {
  2. for (String arg : args) {
  3. System.out.println(arg);
  4. }
  5. }
——————————————————————————————-
BROWSER RELATED
——————————————————————————————
1. quick overview of HTML
  • HTML tags
  • HTML attributes
  • HTML values
  • HTML types of elements
2. quick overview of CSS rules
  1. p {
  2. color: red;
  3. text-align: center;
  4. }
3. quick overview of Javascript
  • find element by id, class name and name
  1. document.getElementById(“id123”)
  2. document.getElementsByClassName("form-submit")
  3. document.getElementsByName("search")
4. whats the browser DOM?
5. use browser inspectors to inspect HTML info of elements
  • Chrome Inspector
  • Firebug/Firepath
6. create XPATH locators for web elements; test them in browser inspectors
  1. //div[@testid = ‘abc’]
  2. //span[@class=’price’]
7. create CSS locators for web elements; test them in browser inspectors
  1. a[href^="https"]
  2. [title~=flower]
—————————————————————————————-
TESTNG
—————————————————————————————-
1. add TESTNG library to the project
  • right click on the project name
  • select Properties
  • click Java Build Path
  • click Libraries
  • click Add Library
  • select TESTNG
  • save
2. use TESTNG unit tests
  1. @Test
  2. public void testSearch() {
  3. driver.get(siteUrl);
  4. ....................
  5. ....................
  6. }
3. use TESTNG fixtures
  1. WebDriver driver;
  2.  
  3. @BeforeMethod
  4. public void setUp() {
  5. driver = new FirefoxDriver();
  6. }
  7.  
  8. @AfterMethod
  9. public void tearDown() {
  10. driver.quit();
  11.  
  12. @Test
  13. public void testSearch() {
  14. driver.get(siteUrl);
  15. ....................
  16. ....................
  17. }
4. use TESTNG assertions
  1. @Test
  2. public void testSearch() {
  3. driver.get(siteUrl);
  4. WebElement element = driver.findElement(By.id("id123"));
  5. assertTrue(element.isDisplayed() == true);
  6. assertEquals(element.getText(), "element value");
  7. ....................
  8. ....................
  9. }
5. run the TESTNG unit tests through the testng.xml file
  1. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  2. <suite name="HighPriority" >
  3. <test name="HighPriorityTests" >
  4.  
  5. <classes>
  6. <class name="tests.UIAutomation.CartTests" />
  7. <class name="tests.UIAutomation.CheckoutTests"/>
  8. </classes>
  9. </test>
  10. <test name="MediumPriorityTests" >
  11.  
  12. <classes>
  13. <class name="tests.FunctionalAutomation.LoginTests" />
  14. <class name="tests.FunctionalAutomation.MyAccountTests"/>
  15. </classes>
  16. </test>
  17. </suite>
6. use DataProviders for unit tests with parameters
  1. @DataProvider(name = "browserNames")
  2. public Object[][] browserNames() {
  3. return new Object[][] {
  4. {"Chrome"},
  5. {"Firefox"}
  6. };
  7. }
  8. public void createDriver(String browserName) throws Exception
  9. {
  10. if (browserName.equalsIgnoreCase("Firefox") >= 0)
  11. driver = new FirefoxDriver();
  12. if (browserName.equalsIgnoreCase("Chrome") >= 0) {
  13. System.setProperty("webdriver.chrome.driver",
  14. "C:\\BrowserDrivers\\chromedriver.exe");
  15. driver = new ChromeDriver();
  16. }
  17. }
  18. @Test(dataProvider="browserNames")
  19. public void openSite(String browser) throws Exception {
  20. createDriver(browser);
  21. driver.get("http://www.autotrader.ca");
  22. driver.quit();
  23. }
7. use TestNG test listeners for taking screenshots
8. use TestNG test listeners for creating test execution reports
9. skip tests using the SkipException exception
10. add timeouts to the tests
11. add the tests to groups
12. use a Test Base Class for the test fixtures
  1. public class TestBase {
  2. WebDriver driver;
  3. @BeforeClass
  4. public void setUp() {
  5. driver = new FirefoxDriver();
  6. }
  7. @AfterClass
  8. public void tearDown() {
  9. driver.quit();
  10. }
  11. }
  12. public class TestClass extends TestBase {
  13. String url = "Vancouver Public Library |";
  14. @Test
  15. public void testScript1() {
  16. driver.get(url);
  17. }
  18. }
——————————————————————————————
SELENIUM
——————————————————————————————
1. download the Selenium library files
  • Selenium Standalone Server jar file
  • Selenium Java Bindings
  • chrome driver
  • gecko driver
2. attach the Selenium library files to the project
  • right click on the project name
  • select Properties
  • click Java Build Path
  • click Libraries
  • click Add External Jars
3. create the driver object and open the site
  1. System.setProperty("webdriver.chrome.driver", "c:/selenium/chromedriver.exe");
  2.  
  3. WebDriver driver = new ChromeDriver();
  4. driver.get(“http://www.bestbuy.com”);
4. get the page title and url
  1. driver.get(“http://www.bestbuy.com”);
  2. String pageTitle = driver.getTitle();
  3. String pageUrl = driver.getCurrentUrl();
5. maximize the browser window; display the browser window in fullscreen mode
6. find an element and get its value and attributes
  1. WebElement element = driver.findElement(By.id(“elementId”));
  2. String elementText = element.getText();
  3. String classValue = element.getAttribute(“class”);
7. check if an element is displayed, enabled and selected
  1. WebElement element = driver.findElement(By.id(“elementId”));
  2.  
  3. boolean isDisplayed = element.isDisplayed();
  4. boolean isEnabled = element.isEnabled();
  5. boolean isSelected = element.isSelected();
8. type in an element
  1. WebElement element = driver.findElement(By.id(“elementId”));
  2.  
  3. element.clear();
  4. element.sendKeys("java");
9. select an option of a listbox
  1. WebElement listElement = driver.findElement(By.id(“listId”));
  2.  
  3. Select list = new Select(listElement);
  4. list.selectByVisibleTest("option1");
10. find multiple elements
  • check how many elements are found
  • get a specific element
  • browse through the elements
  1. By locator = By.classname(“abc”);
  2. List<WebElement> elements = driver.findElements(locator);
  3.  
  4. int elementsCount = elements.size();
  5.  
  6. WebElement element1 = elements.get(0);
  7.  
  8. for (int i = 0; i < elements.size(); i++) {
  9. WebElement e = elements.get(i);
  10. System.out.println(e.getText());
  11. }
11. use explicit waits and expected conditions
  1. WebDriverWait wait = new WebDriverWait(driver, 10);
  2.  
  3. WebElement element = wait.until(
  4. ExpectedConditions.
  5. visibilityOfElementLocated(
  6. By.id("elementId"));
See on this link a detailed article about how to use explicit waits and expected conditions.
15. Use fluent waits
16. execute Javascript code
  1. WebDriver driver = new FirefoxDriver();
  2. JavascriptExecutor js = (JavascriptExecutor)driver;
  3. js.executeScript("return document.getElementById('someId');");
17. take screenshots
  1. WebDriver driver=new FirefoxDriver();
  2. File src= ((TakesScreenshot)driver). getScreenshotAs
  3. (OutputType. FILE);
  4. FileUtils. copyFile(src, new File("C:/selenium/screenshot.png"));
18. use frames
  1. driver.get("http://abc.com");
  2. driver.manage().window().maximize();
  3.  
  4. driver.switchTo().frame("frame1");
  5. driver.findElement(By.xpath(locator)).click();
  6.  
  7. driver.switchTo().defaultContent();
19. use browser tabs
  1. String parentId = driver.getWindowHandle();
  2.  
  3. String link = driver.findElement(locator).click();
  4.  
  5. for (String winId : driver.getWindowHandles())
  6. if (winId.equalsIgnoreCase(parentId) == false) {
  7. driver.switchTo().window(winId);
  8. break;
  9. }
  10.  
  11.  
  12. .....................................
  13.  
  14. driver.close();
  15. driver.switchTo().window(parentId);
20. use ChromeOptions
  1. ChromeOptions chromeOptions = new ChromeOptions();
  2. chromeOptions.addArguments("--start-maximized");
  3. driver = new ChromeDriver(chromeOptions);
21. use DesiredCapabilities
  1. DesiredCapabilities capabilities = DesiredCapabilities.chrome();
  2.  
  3. Proxy proxy = new Proxy();
  4. proxy.setHttpProxy("myhttpproxy:3337");
  5. capabilities.setCapability("proxy", proxy);
  6.  
  7. ChromeOptions options = new ChromeOptions();
  8. options.addExtensions(new File("/path/to/extension.crx"));
  9. capabilities.setCapability(ChromeOptions.CAPABILITY, options);
  10. ChromeDriver driver = new ChromeDriver(capabilities);
22. Run tests in all major browsers (Chrome, Firefox, IE, Edge)
  1. public class HomePage {
  2. private By SEARCH_BOX_ID = By.id(“aaaa”);
  3. private By SEARCH_BUTTON_ID = By.id(“bbbb”);
  4. private String TITLE = home page title”;
  5. private String URL = http://www,abc.com”;
  6. private WebDriver driver;
  7.  
  8. public HomePage(WebDriver driver) {
  9. this.driver = driver;
  10. }
  11.  
  12. public void open() {
  13. driver.get(URL);
  14. if (driver.getTitle).equalsIgnoreCase(TITLE) == false)
  15. throw new RuntimeException(“home page is not displayed!”);
  16. }
  17.  
  18. public void search(String keyword) {
  19. WebElement searchBox = driver.findElement(SEARCH_BOX_ID);
  20. searchBox.clear();
  21. searchBox.sendKeys(keyword);
  22. WebElement searchButton = driver.findElement(SEARCH_BUTTON_ID);
  23. searchButton.click();
  24. }
  25.  
  26. ………………………............................
  27.  
  28. @Test
  29. public void searchWorks() {
  30. HomePage homePage = new HomePage(driver);
  31. homePage.open();
  32. homePage.search(“java”);
  33. }
24. create not only page objects but also page elements
25. use Page Factory
  1. ------ PAGE OBJECT CLASS ------------
  2. package org.openqa.selenium.example;
  3.  
  4. import org.openqa.selenium.By;
  5. import org.openqa.selenium.support.CacheLookup;
  6. import org.openqa.selenium.support.FindBy;
  7. import org.openqa.selenium.support.How;
  8. import org.openqa.selenium.WebElement;
  9.  
  10. public class GoogleSearchPage {
  11. @FindBy(how = How.NAME, using = "q")
  12. @CacheLookup
  13. private WebElement searchBox;
  14.  
  15. public void searchFor(String text) {
  16. searchBox.sendKeys(text);
  17. searchBox.submit();
  18. }
  19. }
  20.  
  21. ------ TEST CLASS ----------------
  22.  
  23. package org.openqa.selenium.example;
  24.  
  25. import org.openqa.selenium.WebDriver;
  26. import org.openqa.selenium.WebElement;
  27. import org.openqa.selenium.htmlunit.HtmlUnitDriver;
  28. import org.openqa.selenium.support.PageFactory;
  29.  
  30. public class UsingGoogleSearchPage {
  31. public static void main(String[] args) {
  32. WebDriver driver = new HtmlUnitDriver();
  33.  
  34. driver.get("http://www.google.com/");
  35.  
  36. GoogleSearchPage page = PageFactory.initElements(
  37. driver,
  38. GoogleSearchPage.class);
  39.  
  40. page.searchFor("Cheese");
  41. }
  42. }
26. load Chrome with extensions
  1. String pathToExtension =
  2. "C:\\Users\\home\\AppData\\Local\\” +
  3. "Google\\Chrome\\User Data\\Default\\Extensions" +
  4. "\\mbopgmdnpcbohhpnfglgohlbhfongabi\\2.3.1_0";
  5. ChromeOptions options = new ChromeOptions();
  6. options.addArguments("--load-extension=" + pathToExtension);
  7. driver = new ChromeDriver(options);
27. interact with the browser cookies
28. emulate complex gestures using the Actions class
29. interact with sliders
30. create a custom Driver class
31. create custom classes for each type of HTML element
33. read the project parameters from command prompt using Maven
34. handle popups
35. handle random popups
36. create custom locators for locating elements by jquery and javascript
38. generate random data for your tests
40. run tests on multiple browsers and operating systems with Sauce Labs
41. simplify tests using the Loadable Component model
42. synchronize the test with the page using the Slow Loadable Component model
————————————————————————————————-

Author : Alex-Siminiuc-2


5 comments:

  1. https://technomeds.blogspot.com/2020/04/full-hd-1080p-mobile-screen-and-audio.html

    ReplyDelete
  2. Wonderful blog. if you are looking certification or trainingitil 4 leader digital and it strategy course
    visit our websiteitil 4 leader digital and it strategy course

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Enjoyed reading the article above ,really explains everything in detail, the article is very interesting and effective. Wish to see much more like this. Thank you and good luck in the upcoming articles.if you are looking certification or training prince2 agile

    prince2 agile training in india

    knowlathon.com

    ReplyDelete