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

Selenium Tutorials

Introduction: Selenium Webdriver Is open source software testing tool which supports many different browsers. It Is also supporting many different software programming languages like Python, Ruby, C#, Java, PHP and Perl so that you can create your test cases using your preferred language. Current days, Most popular language Is Java to prepare your software application automation test cases with selenium webdriver. To get a deep stretch to Selenium concepts I have splited the concepts based on the slow process approach In this tutorial we are going to see what is Selenium Automation testing is all about! before we deep dive into the concepts we will have few Introduction about "Automation Testing" the difference between Test case and Test scripts and different tools in Market. Automation Testing: It is a process of converting manual Test cases to Automated Test scripts by using an Automation Tool is called Automation Testing. Difference between Test Cas...

Customer Expects SIT in full swing!

To start with SIT - System Integration Testing "We" as software Testers should be performing at-least one round of Integration Testing before giving sign-off to any Product/Major Release. System Integration Testing plays a major role in modern era as customers expect us to Test the software in whole, as in they are not satisfied with just the UI validations or testing the document requirements which is developed on the page. Software Applications now a days are developed with lots and lots of dependencies. To perform or complete any action by the software it requires many or third party involvement. Lets make it simple take an eCommerce website(say Amazon), For Amazon to display a set of products on their site there are n number of Services which are playing a major role in the background. Lets make it even more simpler with an Example. Test Scenario: Check if the user is able to view the price for a product. Normal Functional Test: In this we just validate and ...