Sunday, January 28, 2018

Selenium Webdriver commands list part-2

1. Command getWindowHandle() and getWindowHandles():- This command is use to store the current window and handle the more than one window at a time when we need to perform some actions on newly opened window.

      String winHandleBefore = driver.getWindowHandle(); // Store the current window on which we are currently focused.

      Set <String> allwin = driver.getWindowHandles();     // Handle the newly opened window.
      driver.switchTo().window(allwin);   // Move on newly opened window, now we can perform any action on this page


2. Create Alert using java script executor Interface:

    JavascriptExecutor javasrc = (JavascriptExecutor)driver;
    javasrc.executeScript("alert('Please enter username.');");


3. Capture Screenshot:

    File srcshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); // Taking screenshot      using this command

FileUtils.copyFile(srcshot, new File("E/:result.jpg")); // save screenshot name as result in E           drive.


4. Generate Mouse Hover Event: Using Actions class we can perform mouse hover operation.

    Actions actions = new Actions(driver);
  WebElement mainMenu = driver.findElement(By.xpath("xpathOfWebElement"));
actions.moveToElement(mainmenu);
actions.perform();


5. Check whether Element is Enabled or Disabled :

     boolean username = driver.findElement(By.xpath("//input[@name='username']"));

It return true/false value based on element is enabled/disabled.


6. Assert Commands:

      assertEquals:-
Assert.assertEquals(actual,expected);


assertNotEquals:-
Assert.assertNotEquals(actual,expected);


assertTrue:-
Assert.assertTrue(condition);


assertFalse:-
Assert.assertFalse(condition);



7. Submit() method:

    driver.findElement(By.xpath("//input[@name='User Details']")).submit();

Note: 1.It is looking button type should be 'submit'. If button type is submit then it will work.
2. Button should be inside the form tag. if it is not inside then it will not work.



8. Handle Javascript Alert:

     Alert alert = driver.SwitchTo().alert(); // Alert is an interface.

alert.accept();   // click on OK button on alert.

  alert.dismiss();  // click on cancle button on alert.

   alert.getText();  // Get text from alert popup.

alert.sendKeys(); // Pass text in alert's text field



9. Select values from dropdown: There are 3 different methods to select values from dropdown.

    1. Select by visible text:


  Select mydropdown = new Select(driver.findElement(By.id("putdropdownid")));
  mydropdown.selectByVisibleText("Loan Account"); // Now Loan Account will be selected.

    2. Select by value:


  Select mydropdown = new Select(driver.findElement(By.id("putdropdownid")));
      mydropdown.selectByValue("Loan Account"); // Now Loan Account will be selected.This          value is taken from HTML of the page.

 

   3. Select by Index:


     Select mydropdown = new Select(driver.findElement(By.id("putdropdownid")));
     mydropdown.selectByIndex(0); //Now zero index option(first option) will be selected.


10. Command get the attribute of any element:


     <input name="Name Locator" value="selenium">Hello</input>

     driver.findElement(By.name("Name Locator")).getAttribute("value"); // It will get the attribute    from the HTML tag. It will return Selenium.




                                                                Selenium Webdriver Commands Part -1



    

No comments:

Post a Comment