Wednesday, July 18, 2018

Must know Selenium Commands with Example





Below are the commands, being selenium automation engineer you should know. These are actions most of the time we use in our daily scripting.









  1. IE Browser Opening:


          System.setProperty("webdriver.ie.driver",PATH);

          WebDriver driver = new InternetExplorerDriver();


          Where PATH = path of IEDriver exe file


         Example:

         System.setProperty("webdriver.ie.driver", “D:\IEDriver.exe”);

         WebDriver driver = new InternetExplorerDriver();



  2.  How to Open Chrome Browser:




          System.setProperty("webdriver.chrome.driver",PATH);

          WebDriver driver = new ChromeDriver();

         

         Where PATH = path of ChromeDriver exe file


         Example:

               System.setProperty("webdriver.chrome.driver",”D:\drivers\chromedriver.exe”);

              WebDriver driver = new ChromeDriver();



 3.  Firefox Browser Opening:


           System.setProperty("webdriver.gecko.driver",PATH);

           WebDriver driver = new FirefoxDriver();


           Where PATH = path of geckodriver exe file


         Example:


           System.setProperty("webdriver.gecko.driver",”D:\driver\geckodriver.exe”);

           WebDriver driver = new FirefoxDriver();


 4.   Finding single element:


      driver.findElement(locator)

      where Locator is a location of element. Locator can be id, name, class name, xpath , css    selector etc..


     Example:


      Below example for finding search box element in google.com


     Driver.findElement(By.name(“q”));


5.  Finding Multiple Elements:




     driver.findElements(locator)


     where Locator is a location of element.


     Locator can be id, name, class name, xpath , css selector etc..


     Example:


     Below example for finding all the links in google.com


     driver.findElements(By.xpath(“//a”));



       6.   Different Types of Locators:


         By ID:

driver.findElement(By.id(str));


where Str  is id of element.


Example: below example to identify the Google logo by id in the google home page


Driver.findElement(By.id(“hplogo”));


         By Name:

            driver.findElement(By.name(str));


            where Str  is name of element.


           Example:

           Below example for finding search box element in google.com


            Driver.findElement(By.name(“q”));



               By class name:
                  driver.findElement(By.className(str));
                  where Str  is class value of element.
     

          Example:

          Below example for identify the google search box by class name:


          Driver.findElement(By.className(“gLFyf gsfi”));



              By css selector:
                driver.findElement(By.cssSelector(str));
                Where Str  is cssSelector of element
          
               Example: Below example for identify the google search box by CSS selector:
  
         Driver.findElement(By.cssSelector(“.gLFyf.gsfi”));


        By link text:

         driver.findElement(By.linkText(str));

          where Str  is link text of element


        Example:


        Below example for identifying the “Gmail” link by link text.


driver.findElement(By.linkText(“mail”));



            By partial link text: 

         driver.findElement(By.partialLinkText(str));

        where Str  is partial text of element


        Example:

          Below example for “How Search Works” link by partial link text.


         driver.findElement(By.partialLinkText(“howsearch”);



           By tag name:

  
            driver.findElement(By.tagName(str));

      where Str  is tag name of element


      Example:  Below example for identifying all the links by tag “a”

      driver.findElement(By.tagName(“a”));



         By XPath:

    driver.findElement(By.xpath(xpath));


    Str  is xpath of element


    Example:


    Below example for identifying “Google Search” button on google home page by xpath


    Driver.findElement(By.xpath(“(//input[@value='Google Search'])[2]”));


       7.   Write in textfields:



      driver.findElement(locator).sendKeys(Text);


      where Text: what you want to write

                 locator: is a location element


     Example: below example for entering “Mahantesh Hadimani blog” in google search box


     Driver.findElement(By.name(“q”)).sendKeys(“Mahantesh Hadimani blog”);


       8. Click button or click radio button or Check checkboxes:



           driver.findElement(locator).click();

           locator is a location element


       9. Clear text in text field:


         driver.findElement(locator).clear();

         locator is a location element


     Example:

     Below example for clearing the search box in the Google home page.


    Driver.findElement(By.name(“q”)).clear();


    Before using sendkeys() method, it is advised to use clear() method.




      10.  Navigate back in browser:


        driver.navigate().back();



      11.  Navigate forward in browser:


      driver.navigate().forward();


  12 .  Navigate to frame:


      driver.switchTo().frame(frame);


      frame can be integer value represents position of frame or string  represents id of frame or   WebElement represents frame of frame.


13.  Navigate to next window or pop up window:



     driver.switchTo().window(hashCode);


     hashCode is hash code of window


14.  Get inner text of element or inner text of table:


     driver.findElement(locator).getText();

     locator is a location element


15.  Opening the URL:


     driver.get(url);


16.  Closing the current browser:



     driver.close();


17.  Closing the all the windows opened in the current session:



     driver.quit();


18. Implicitly wait:



driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);


 this command will be used put wait before executing next command. This command will be useful to wait for given number of seconds to load page before executing command

19. Get page title:  

   The command is used to retrieve the title of the webpage the user is currently working on.

   Example:
   String title = driver.getTitle();

20. Get text: 


    The command is used to retrieve the inner text of the specified web element

    Example:
   String Text = driver.findElement(By.id("username")).getText();

21. Get Current Page Url:  


    This method gets the current page url. We usually use this method in commands to check if we have navigated to the right page as expected

   Example:
  driver.getCurrentUrl();

22. isEnabled()

    To check if a particular element is enabled in a web page or not, we use isEnabled() method.

   Example:
   boolean loginBtn = driver.findElement(By.xpath("//input[@name='Login']")).isEnabled();

23. Navigate(): 


   To navigate from landing page url and the go back or farward we use this command.

   Example:

   Navigate:
   Navigate to "https://mahantesh-hadimani.blogspot.com":
   driver.navigate().to("https://mahantesh-hadimani.blogspot.com");

   Navigate back:
   driver.navigate().back();

   Navigate to forward:
   driver.navigate.forward();

24. moveToElement() : 


  moveToElement() command used to simulate mouse hover effect.

  When we need to mouse hover over web elements like over menu to see submenu, links to see color changes etc. In these cases, we use Actions class.

  Example:

  Actions actions = new Actions(driver);

 WebElement mouseHoverElement = driver.findElement(By.xpath("//div[@id='HeadeMenu1']"));

  actions.moveToElement(mouseHoverElement );

  actions.perform();

25. dragAndDrop() : 


dragAndDrop() command used to drag an element and drop it on another element.

Example, drag an image to the some location. In this case, we can use the this command.



Example:

WebElement sourceElement = driver.findElement(By.xpath("//*[@id='image1']/a"));
WebElement destElement = driver.findElement(By.xpath("//*[@id='stage']/li"));
Actions actions=new Actions(driver);
actions.dragAndDrop(sourceElement, destElement).build().perform();

In the dragAndDrop method, we pass the two parameters.

Source locator- the element we want to drag and
Destination locator- the element to which we want to drop.

26. Alerts methods :  


  switchTo() , accept(), dismiss() and sendKeys() - These methods will be used to switch to alert and handle them like disimissing, accepting and sending text to alerts.

Example:

SwitchTo:
Alert alert = driver.switchTo().alert();

Here we are switching to the alert

sendKeys:

Alert alert = driver.switchTo().alert();
alert.sendKeys("This Is Softwaretestinghelp");

Here we are sending text to the alert.

Accept:
Alert alert = driver.switchTo().alert();
alert.accept()

here we are accepting the alert.
Dismiss:
Alert alert = driver.switchTo().alert();
alert.dismiss()

here we are dismissing the alert.

27. getScreenshotAs() : this method used to take page sceenshot. usually we use this most of the times when test script failed. so that we can get to know why test script failed


Example:

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

28. getPageSource(): this method is used to retrieve the source code of the current page. it returns as string.

Example:

String pageSource = driver.getPageSource();

since it returns in string, we need to store in String.


No comments:

Post a Comment

If any suggestions or issue, please provide