Selenium Selectors

Selenium offers a number of build in selectors to easy find elements in the application under test

class nameLocates elements whose class name contains the search value (compound class names are not permitted)
css selector Locates elements matching a CSS selector
idLocates elements whose ID attribute matches the search value
nameLocates elements whose NAME attribute matches the search value
link textLocates anchor elements whose visible text matches the search value
partial link textLocates anchor elements whose visible text contains the search value. If multiple elements are matching, only the first one will be selected.
tag nameLocates elements whose tag name matches the search value
xpathLocates elements matching an XPath expression

Preferred Selector Order

id > name >links text> css > xpath

  // locate google search field using ID
        driver.findElement(By.id("lst-ib"));
        // locate google search field using name
        driver.findElement(By.name("q"));
        // locate google search field using Class name
        driver.findElement(By.className("gsfi"));
        // locate google search field using css selector
        driver.findElement(By.cssSelector("#lst-ib"));
        // locate google search field using link Text
        driver.findElement(By.linkText("Gmail"));
        // locate google search field using partial link text
        driver.findElement(By.partialLinkText("mail"));
        // locate google search field using tag name
        driver.findElement(By.tagName("input"));
        // locate google search field using xpath
        driver.findElement(By.xpath("//*[@id=\"lst-ib\"]"));

Selenium Waits

public class Waits {


    /**
     * Implicit Wait
     * The implicit wait will tell to the web driver to wait for certain amount of time before it throws a "No Such Element Exception". The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception.
     * In the below example we have declared an implicit wait with the time frame of 10 seconds. It means that if the element is not located on the web page within that time frame, it will throw an exception.
     */
    @Test
    public void implicitWait(){
        // set driver system path
        System.setProperty("webdriver.gecko.driver", StartBrowsers.pathToDrivers+"geckodriver.exe");
        // initialise browser
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
    }

    /**
     * The explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an "ElementNotVisibleException" exception.
     * The explicit wait is an intelligent kind of wait, but it can be applied only for specified elements. Explicit wait gives better options than that of an implicit wait as it will wait for dynamically loaded Ajax elements.
     */
    @Test
    public void explicitWait(){
        // set driver system path
      setProperty("webdriver.gecko.driver", StartBrowsers.pathToDrivers+"geckodriver.exe");
        // initialise browser
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id( "someId")));
    }


}

JUnit Annotations Explained

@Before is executed before each test,

@BeforeClass runs once before the entire test fixture.

If your test class has ten tests, @Before code will be executed ten times, but @BeforeClass will be executed only once.

In general, you use @BeforeClass when multiple tests need to share the same computationally expensive setup code. Establishing a database connection falls into this category. You can move code from @BeforeClass into @Before, but your test run may take longer. Note that the code marked @BeforeClass is run as static initializer, therefore it will run before the class instance of your test fixture is created.

Selenium Hello World

A “Hello, World!” program generally is a computer program that outputs or displays the message “Hello, World!”. Such a program is very simple in most programming languages, and is often used to illustrate the basic syntax of a programming language. It is often the first program written by people learning to code.[1][2] It can also be used as a sanity test to make sure that a computer language is correctly installed, and that the operator understands how to use it.

https://en.wikipedia.org/wiki/%22Hello,_World!%22_program

With that in mind, a Selenium Hello World is a simple test, of checking the title on a page.

public class HelloWorld {
    WebDriver driver;
    @Before
    public void setUp(){
        // System Property for Gecko Driver
        System.setProperty("webdriver.gecko.driver","path to koDriver\\geckodriver.exe" );
        // Initialize Gecko Driver 
        driver = new FirefoxDriver();
    }
    @Test
    public void helloWorldSelenium(){
        // Open Google Page
        driver.get("https://www.google.com/");
        // Get Page Title
        String pageTitle = driver.getTitle();
        // Assert that the title is Google
        assertEquals("Google",pageTitle);
     
    }
    @After
    public void teardown(){
        // close the driver
        driver.quit();
    }
}