Skip to main content

Customer Expects SIT in full swing!

Selenium - Basic Commands


  1. Creating New Instance
  2. WebDriver driver = new FirefoxDriver()

    The above command is used to open a new instance of Firefox driver
  3. Command To Open URL In Browser
  4. driver.get("http://only-testing-blog.blogspot.com/2013/11/new-test.html");

    This syntax will open specified URL of software web application in web browser.

  5. Clicking on any element or button of webpage
  6. driver.findElement(By.id("submitButton")).click();

    Above given syntax will click on targeted element in webdriver.

  7. Store text of targeted element in variable
  8. String dropdown = driver.findElement(By.tagName("select")).getText();

    This syntax will retrieve text from targeted element of software web application page and will store it in variable = dropdown.

  9. Typing text in text box or text area.
  10. driver.findElement(By.name("fname")).sendKeys("My First Name");

    Above syntax will type specified text in targeted element. VIEW PRACTICAL EXAMPLE OF SendKeys

  11. Applying Implicit wait in webdriver
  12. driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

    This syntax will force webdriver to wait for 15 second if element not found on page of software web application.

  13. Applying Explicit wait in webdriver with WebDriver canned conditions.
  14. WebDriverWait wait = new WebDriverWait(driver, 15); wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds"));

    Above 2 syntax will wait for till 15 seconds for expected text "Time left: 7 seconds" to be appear on targeted element.

  15. Get page title in selenium webdriver
  16. driver.getTitle();

    It will retrieve page title and you can store it in variable to use in next steps. VIEW PRACTICAL EXAMPLE OF GET TITLE
  17. Get Current Page URL In Selenium WebDriver
  18. driver.getCurrentUrl();

    It will retrieve current page URL and you can use it to compare with your expected URL. VIEW PRACTICAL EXAMPLE OF GET CURRENT URL
  19. Get domain name using java script executor
  20. JavascriptExecutor javascript = (JavascriptExecutor) driver; String CurrentURLUsingJS=(String)javascript.executeScript("return document.domain");

    Above syntax will retrieve your software application's domain name using webdriver's java script executor interface and store it in to variable. VIEW GET DOMAIN NAME PRACTICAL EXAMPLE.
  21. Generate alert using webdriver's java script executor interface
  22. JavascriptExecutor javascript = (JavascriptExecutor) driver; javascript.executeScript("alert('Test Case Execution Is started Now..');");

    It will generate alert during your selenium webdriver test case execution. VIEW PRACTICAL EXAMPLE OF GENERATE ALERT USING SELENIUM WEBDRIVER.
  23. Selecting or Deselecting value from drop down in selenium webdriver.
  24. Select By Visible Text

    Select mydrpdwn = new Select(driver.findElement(By.id("Carlist"))); mydrpdwn.selectByVisibleText("Audi");

    It will select value from drop down list using visible text value = "Audi". Select By Value

    Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']"))); listbox.selectByValue("Italy");

    It will select value by value = "Italy". Select By Index

    Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']"))); listbox.selectByIndex(0);

    It will select value by index= 0(First option). VIEW PRACTICAL EXAMPLES OF SELECTING VALUE FROM DROP DOWN LIST. Deselect by Visible Text

    Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']"))); listbox.deselectByVisibleText("Russia");

    It will deselect option by visible text = Russia from list box. Deselect by Value

    Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']"))); listbox.deselectByValue("Mexico");

    It will deselect option by value = Mexico from list box. Deselect by Index

    Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']"))); listbox.deselectByIndex(5);

    It will deselect option by Index = 5 from list box. Deselect All

    Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']"))); listbox.deselectAll();

    It will remove all selections from list box of software application's page. VIEW PRACTICAL EXAMPLES OF DESELECT SPECIFIC OPTION FROM LIST BOX isMultiple()

    Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']"))); boolean value = listbox.isMultiple();

    It will return true if select box is multiselect else it will return false.VIEW PRACTICAL EXAMPLE OF isMultiple() 13. Navigate to URL or Back or Forward in Selenium Webdriver

    driver.navigate().to("http://only-testing-blog.blogspot.com/2014/01/textbox.html");

    driver.navigate().back();

    driver.navigate().forward();

    1st command will navigate to specific URL, 2nd will navigate one step back and 3rd command will navigate one step forward. VIEW PRACTICAL EXAMPLES OF NAVIGATION COMMANDS. 14. Verify Element Present in Selenium WebDriver

    Boolean iselementpresent = driver.findElements(By.xpath("//input[@id='text2']")).size()!= 0;

    It will return true if element is present on page, else it will return false in variable iselementpresent. VIEW PRACTICAL EXAMPLE OF VERIFY ELEMENT PRESENT. 15. Capturing entire page screenshot in Selenium WebDriver

    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("D:\\screenshot.jpg"));

    Takes the screen shot of the page and stores in D:// in our local machine.

     16. Generating Mouse Hover Event In WebDriver

    Actions actions = new Actions(driver); WebElement moveonmenu = driver.findElement(By.xpath("//div[@id='menu1']/div")); actions.moveToElement(moveonmenu); actions.perform(); 

     Above example will move mouse on targeted element. VIEW PRACTICAL EXAMPLE OF MOUSE HOVER. 17. Handling Multiple Windows In Selenium WebDriver. Get All Window Handles.

Comments

Popular posts from this blog

Software Testing - Overview

Hi Friends, Welcome to a quick look up site, where you can know what is "T" "E" "S" "T" "I" "N" "G"! This site gives you the Overview as well the in-depth details of Software Testing, Approaches, Practices and additional tools which are required in Software testing practice. You could have imagined why this guy is speaking like a Professor. You as a reader knows right now a days people want's them to showcase and make things complicated. But I can assure reading this blog site you will enjoy learning in the same time not feel like in living in a 4th Dimensional world  (The Interstellar way ;-) ). Fun's apart! So lets start the story of Software Testing! Before I start I wan't to personally thank my English teacher for making me type a decent English and my Testing Guru Mr. Aravinth Kumar for giving me the complete knowledge of Software testing. To start with the story, we need to know about ab...

TestNG Tutorial

Introduction  TestNG is basically a Unit testing framework basically for Selenium webdriver Software automation for Test engineers. It is basically inspired or developed from JUnit (Tut's coming soon on JUnit) with additional functionalities or features. We use TestNG basically to annotate in the sense to make our code easily understandable to anyone. Its splits up the code in the form on Annotations like @Before , @After to showcase our code written in Selenium. Our section of TestNG here covers the below 1) Installation Guide 2) Execute a TestNG Webdriver Test 3) Testng Annotations with example To know more about TestNG features click on the link TestNG - Features or visit TestNG.org