2 Mart 2016 Çarşamba

selenium java

1. How to click on element: By using click() function we perform click operation on page element:
WebElement el   driver.findElement(By.id(“ElementID”));
el.click();
2. How to enter text into page element: By using sendkeys() function we enter text in page element such as text field, area etc:
WebElement el   driver.findElement(By.id(“ElementID”));
el.clear(); //use to clear input field
el. sendKeys (“User name”);
3. How to count total number rows in table:
List rows = driver.findElements(By.className(“//table[@id=’tableID’]/tr”));
int totalRow = rows.size();


4. How to get page title: We use getTitle() function to get title of page.
String title =  driver.getTitle()
5. How to get page source of webpage: we use getPageSource() function to get page source.
String pagesource = driver.getPageSource()
6. How to get current url : we use driver.getCurrentUrl() function to get current url
String currentURL  = driver.getCurrentUrl()
7. How to assert text of webpage:  we get attribute by using getText () method.
WebElement el   driver.findElement(By.id(“ElementID”));
//get test from element and stored in text variable
String text = el.getText();
//assert text from expected
Assert.assertEquals(“Element Text”, text);
8. How to get element attribute: we get attribute by using getAttribute() method.
WebElement el  =  driver.findElement(By.id(“ElementID”));
//get test from element and stored in text variable
String attributeValue = el. getAttribute(“AttributeName”) ();
//assert text from expected
Assert.assertEquals(“Attribute Value”, attributeValue);
9. How to double click on element: we use Action class object doubleClick() method to perform this.
WebElement el  =  driver.findElement(By.id(“ElementID”));
Actions builder = new Actions(driver);
builder.doubleClick(el).build().perform();
10. How to perform drag and drop: we will use Action class object doubleClick method to perform this.
WebElement source  =  driver.findElement(By.id(“Source ElementID”));
WebElement destination  =  driver.findElement(By.id(“Taget ElementID”));
Actions builder = new Actions(driver);
builder.dragAndDrop(source, destination ).perform();
11. How to execute java script code: To execute java script code we use JavascriptExecutor class.
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript(“pass your java scripts”);
12. How to capture screen shot: we use TakesScreenshot interface to get page screen shot.
File file= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(“c:\\name.png”));
13. How to maximize window: we use windowMaximize() to maximize window.
driver.manage().window().maximize();
14. How to automate check boxes: using below code we can click on check box and verifying check box is selected etc.
WebElement el = driver.findElement(By.id(“element Id”));
//to perform check operation
el.click()
//verfiy to check box it return true if selected else false
el.isSelected()
15. How to automate radio button: : using below code we can click on check box and verifying check box is selected etc.
WebElement el = driver.findElement(By.id(“Radio button id”));
//to perform check operation
el.click()
//verfiy to radio button is check it return true if selected else false
el.isSelected()
16. How to handle drop down: by using select class we can automate dropdown following are the some main operation which we perform on drop down list.
By using selectByVisibleText() method of Select class we can select visible text of dropdown.
Select sel= new Select(driver.findElement(By.id(“drop down ID”)));
sel.selectByVisibleText(“Pass your desire Text”);
 Select drop down by value
Select sel= new Select(driver.findElement(By.id(“drop down ID”)));
sel. selectByValue( (“Pass your desire Text”);
 Select drop down by index
Select sel= new Select(driver.findElement(By.id(“drop down ID”)));
sel. selectByIndex (0);  // 0 is first index
 Verify dropdown supported multiple options or not
Select sel= new Select(driver.findElement(By.id(“drop down ID”)));
Boolean status = sel. isMultiple()
Get number of options are available in drop down
Select sel= new Select(driver.findElement(By.id(“drop down ID”)));
int  totalOption  = sel. getOptions().size()