Skip to main content

Most useful commands in Selenium using Java

Here the list of most used commands in selenium with its uses.


get(String value)

    This method is used to launch the URL. Full URL of the application to test should be passed in this method

                String landingURL = "https://www.facebook.com/";
                driver.get(landingURL);

getCurrentURL()

    This method is used to get the current site URL. No need to pass the parameter to this method and it returns a string of the current URL.

                driver.getCurrentUrl();

getTitle()

    This method is used to get the page title. This method passes nothing and returns a String value of the title

                driver.getTitle()

manage().window().maximize()

    This command is used to maximize the browser window. There are some more methods that will be used from the Window class.

                driver.manage().window().maximize();

manage().window().getSize()

    This method will return the dimensions of the browser.

                driver.manage().window().getSize();

close()

    This method is used to close the current tab. 

                driver.close();

getWindowHandle()

    In many scenarios script will not be run in a single window. There will be a need for additional windows to run some scenarios. In that case, switching between the windows is necessary. This getWindowHandle method is used to simplify that process.  This method will be used to get the unique ID of the current window. Then by using the switchTo() method we can able to switch across the parent window and child window. 

                String parentWindow = driver.getWindowHandle();
                driver.switchTo().window(parentWindow);

getWindowHandles()

    By using this method, we can able to store unique IDs for all the tabs. After that those unique IDs are iterated and do necessary actions on the corresponding window. This will be used along with the getWindowHandle method to differentiate the child tab from the parent tab.

                String parentWindow = driver.getWindowHandle();
                Set<String> windows = driver.getWindowHandles();

                Iterator<String> i1 = windows.iterator();
                while (i1.hasNext()) {
                String ChildWindow = i1.next();

                    if (!parentWindow.equalsIgnoreCase(ChildWindow)) {
                    driver.switchTo().window(ChildWindow);
                    -------Code that needs to perform in the tab--------
                    }

                }

switchTo().alert()

    The below mentioned commands are useful in scenarios that crossing different type of pop-up or alert messages. 

    driver.switchTo( ).alert( ).accept(); - This command is used to accept the alert or popup window
    driver.switchTo( ).alert( ).dismiss(); -  This command is used to cancel the alert or popup window
    driver.switchTo().alert().getText(); - This command is used to copy the text of the alert or popup window
    driver.switchTo().alert().sendKeys("Name"); - This command is used to pass any value to the alert or popup window

Sample Program with all commands


public class Login {
public static void main(String[] args) throws AWTException {
System.setProperty("webdriver.chrome.driver","F:\\Selenium drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Actions actions = new Actions(driver);
Robot robot = new Robot();
String landingURL = "https://www.amazon.com/";
String userName = "qatest@gmail.com";
String password = "texplorech";
//This command is used to launch the URL
driver.get(landingURL);
//This command is used to get and display the title of the page
System.out.println(driver.getTitle());
//This command is used to get and display the URL of the page
System.out.println(driver.getCurrentUrl());
//This command used to store the element
WebElement myAccountoverlay = driver.findElement(By.cssSelector("a[data-nav-ref='nav_ya_signin']"));
//This command is used to mouse hover the element
actions.moveToElement(myAccountoverlay).perform();
WebElement signIn = driver.findElement(By.cssSelector("span[class=nav-action-inner]"));
//This command is used to click the element
signIn.click();
//This command is used to send a text to the element
driver.findElement(By.name("email")).sendKeys(userName);
driver.findElement(By.id("continue")).click();
driver.findElement(By.cssSelector("input[type='password']")).sendKeys(password);
driver.findElement(By.id("signInSubmit")).click();
//This command are used to press the key
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
//This command is used to get the unique ID of the current window
String parentWindow = driver.getWindowHandle();
//This command is used to get the unique ID for all the tabs on that scenario
Set<String> windows = driver.getWindowHandles();
Iterator<String> i1 = windows.iterator();
while (i1.hasNext()) {
          String ChildWindow = i1.next();
          if (!parentWindow.equalsIgnoreCase(ChildWindow)) {
          
              //This command is used to switch across browser tabs
              driver.switchTo().window(ChildWindow);
              
              //This command is used to close the current tab
              driver.close();
              
              System.out.println("Child window closed");
           }
       }
//This command is used to quit the browser
driver.quit();
}
}

Action class and robot class will be discussed in upcomming posts

Comments