Switching between Iframes

there are cases where an object is located in an Iframe ( web text editors is a common case ) but a common rule is . The iframe is often used to add content from other sources like an advertisement into a web page. The iframe is defined with the <iframe> tag.

When we want to select an element in an Iframe we need to switch webdriver’s focus to that Iframe

Selenium provides

driver.switchTo().frame(WebElement)
// do all your actions here 
// then switch back to the original content 
driver.switchTo().defaultContent(); 

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 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();
    }
}