Selenium Commands

In this article we will cover Selenium commands you should be aware of if you are going for interviews.

Selenium Commands

  • get() Methods -
    • driver.get("https://google.com"); -- used to open an URL and it will wait till the whole page gets loaded.
    • driver.getClass(); -- The command is used to retrieve the Class object that represents the runtime class of this object
    • driver.getCurrentUrl(); -- This command returns the URL of the currently active web page in the browser.
    • driver.getPageSource(); -- This command helps in getting the entire HTML source code of the open web page.
    • driver.getTitle(); -- This command can be used for displaying the title of the current web page.
    • driver.getText(); -- delivers the innerText of a WebElement.
    • driver.findElement(By.id("findID")).getAttribute("Value"); -- used to retrieve the value of the specified attribute
    • driver.getWindowHandle(); -- used to tackle with the situation when we have more than one window to deal with.
  • Locating links by linktext() and partialLinkText() -
    • driver.findElement(By.linkText("Testers")).click(); -- finds the element using link text
    • driver.findElement(By.partialLinkText("Test")).click(); -- find the elements based on the substring of the link
  • Selecting multiple items in a drop down -
    • Select select = new Select(driver.findElement(By.id("Tester")));
    • select.selectByValue("Value");
    • select.selectByVisibletext("Text");
    • select.selectByIndex(2);
  • Submitting a form -
    • driver.findElement(By.id("Submit")).submit();
  • Handling iFrames -
    • select frame by id - driver.switchTo().frame("Id of the frame");
    • Locating iframe using tagName - driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0));
    • Locating iframe using index - driver.switchTo().frame(0);
    • Locating by name of iframe - driver.switchTo().frame("Name of thr frame");
    • select parent window - driver.switchTo().defaultContent();
  • Close() and quit() methods -
    • driver.close(); -- closes only a single window that is being accessed by the WebDriver instance currently
    • driver.quit(); -- closes all the windows that were opened by the WebDriver instance
  • Exception Handling -
    • Exceptions are the conditions or situations that halt the program execution unexpectedly.
    • Reasons for such conditions can be:
      • Errors introduced by the user 
      • Errors generated by the programmer 
      • Errors generated by physical resources
    • Example -
      • WebElement saveButton = driver.findElement(By.id("Save")); 
      • try{ if(saveButton.isDisplayed()){ 
      • saveButton.click();
      • } } 
      • catch(NoSuchElementException e){ 
      • e.printStackTrace(); 
      • }
  • Other useful commands -
    • isEnabled() ; -- to Check Whether the Element is Enabled Or Disabled in the Selenium WebDriver.
      • Syntax - 
        • boolean textBox = driver.findElement(By.xpath("//input[@name='textbox1']")).isEnabled();
    • pageLoadTimeout(time, unit) ; -- to set the time for a page to load.
      • Syntax -
        • driver.manage().timeouts().pageLoadTimeout(500, SECONDS);
    • implicitlyWait(); -- to set a wait time before searching and locating a web element.
      • Syntax -
        • driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);
    • until() and visibilityOfElementLocated() -- until() from WebdriverWait and visibilityOfElementLocated() from ExpectedConditions to wait explicitly till an element is visible in the webpage.
      • Syntax -
        • WebDriverWait wait = new WebDriverWait(driver, 10); 
        • WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated (By.xpath("//input[@id=’name’]")));
    • until() and alertIsPresent() -- untill() from WebdriverWait and alertIsPresent() from ExpectedConditions to wait explicitly till an alert appears. 
      • Syntax
        • WebDriverWait wait = new WebDriverWait(driver, 10); 
        • WebElement element = wait.until(ExpectedConditions.alertIsPresent() ); 
    • Select 
      • WebElement mySelectedElement = driver.findElement(By.id("select")); 
      • Select dropdown= new Select(mySelectedElement);
      •  dropdown.selectByVisibleText("Jaikishan"); 
      •  dropdown.selectByValue("Fav_course"); 
      • dropdown.selectByIndex(1); 
      • dropdown.deselectByVisibleText(“Tester");
      •  dropdown.deselectByValue("Fav_course"); 
      •  dropdown.deselectByIndex(1);
    • Navigate() methods -- to navigate between the URLs. 
      • driver.navigate().to("https://www.testinghackersz.blogspot.com");  
      • driver.navigate().back(); 
      • driver.navigate().forward(); 
      • driver.navigate().refresh();
    • getScreenshotAs() method --to Capture the entire page screenshot in Selenium WebDriver.
      • File shot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
      • FileUtils.copyFile(shot, new File("D:\\criticalbug.jpg"));
    • moveToElement() -
      • Actions actions = new Actions(driver); 
      • WebElement mouseHover = driver.findElement(By.xpath("//div[@id='bestTutor']/div"));
      • actions.moveToElement(mouseHover); actions.perform();
    • dragAndDrop()
      • actions.dragAndDrop(sourceLocator, destinationLocator).build().perform();
    • switchTo() and accept(), dismiss() and sendKeys() -- methods from Alert class to switch to popup alerts and handle them. 
    • Syntax
      • Alert alert = driver.switchTo().alert(); 
      • alert.sendKeys("Don’t forget to give 5 star rating"); 
      • alert.accept();  
      • alert.dismiss() can be used to dismiss the alert.
    • assertEquals(),assertNotEquals(), assertTrue() and assertFalse() -- Assertions are used to compare the expected and actual results. Pass or fail of a test is usually decided from the result of assertions. 
    • Syntax
    • Assert.assertEquals(message, “This text”); 
    • Assert.assertNotEquals(message, “This text”); 
    • Assert.assertTrue(result<0); 
    • Assert.assertFalse(result<0);

Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Testing in CI/CD

Azure Tutorials Series - IaaS vs PaaS vs SaaS