chia sẻ

Basic Action Commands And Operations for Selenium With Examples [Automation Testing]

Basic Action Commands And Operations With Examples



1. Creating New Instance Of Firefox Driver

WebDriver driver = new FirefoxDriver();


2. Command To Open URL In Browser

driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");

3. Clicking on any element or button of webpage

driver.findElement(By.id("submitButton")).click();


4. Store text of targeted element in variable

String dropdown = driver.findElement(By.tagName("select")).getText();


5. Typing text in text box or text area.

driver.findElement(By.name("fname")).sendKeys("My First Name");


6. Applying Implicit wait in webdriver

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


7. Applying Explicit wait in webdriver with WebDriver canned conditions.

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. 

8. Get page title in selenium webdriver

driver.getTitle();

9. Get Current Page URL In Selenium WebDriver

driver.getCurrentUrl();

10. Selecting or Deselecting value from drop down in selenium webdriver.
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).

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.
11. Navigate to URL or Back or Forward in Selenium Webdriver

driver.navigate().to("http://only-testing-blog.blogspot.in/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. 

12. 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. 


13. 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.


14. Check Whether Element is Enabled Or Disabled In Selenium Web driver.

boolean fname = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();
System.out.print(fname);
Above syntax will verify that element (text box) fname is enabled or not. You can use it for any input element. .


15. Selenium WebDriver Assertions With TestNG Framework
·         assertEquals
Assert.assertEquals(actual, expected);
assertEquals assertion helps you to assert actual and expected equal values. 

 assertNotEquals
Assert.assertNotEquals(actual, expected);
assertNotEquals assertion is useful to assert not equal values. 
·         assertTrue
Assert.assertTrue(condition);
assertTrue assertion works for boolean value true assertion. 
·         assertFalse
Assert.assertFalse(condition);
assertFalse assertion works for boolean value false assertion. 

16. Submit() method to submit form

driver.findElement(By.xpath("//input[@name='Company']")).submit();
It will submit the form. 

17. Handling Alert, Confirmation and Prompts Popups


String myalert = driver.switchTo().alert().getText();
To store alert text. 


driver.switchTo().alert().accept();
To accept alert. 


driver.switchTo().alert().dismiss();
To dismiss confirmation. 


driver.switchTo().alert().sendKeys("This Is John");
To type text In text box of prompt popup. 


====================================================================

18. Checkbox

WebElement checkbox = driver.findElement(By.name("vehicle"));
checkbox.click();

WebElement checkbox = driver.findElement(By.xpath(".//*[@id='vehicle']")).click();



===============================================================================
19.  Wait to the website be load successfully

thread sleep (5000);

20.Get page source

System.out.println(driver.getPageSource());

21.  Get page URL

System.out.println("URL: "+driver.getPageSource());

22. Get page title

System.out.println("title: "+driver.getPageSource());

23. Get text on Elements

String contact_result = driver.findElement(By.xpath(".//*[@id='ContactStandardSearch']/div[3]/span")).getText();
System.out.printf("\nNumber of contact results: "+ contact_result);

24. Some special actions

Actions action = new Actions(driver);
WebElement element=driver.findElement(By.linkText("TEST"));

//Double click

action.doubleClick(element).perform();
//Mouse over

action.moveToElement(element).perform();
//Right Click

action.contextClick(element).perform();

25. To driver Maximize window:

driver.manage().window().maximize();

26. To counter number of lines on tables

List<WebElement> Elements = driver.findElements(By.xpath("html/body/table/tbody/tr"));
int xpathCount = Elements.size();
xpathCount = xpathCount -1;
System.out.println("No. of rows" + xpathCount );

27. Some actions using Java script

// To tick on a checkbox

WebElement element_three = driver.findElement(By.id("tick_off"));
JavascriptExecutor executor_2 = (JavascriptExecutor)driver;
executor_2.executeScript("arguments[0].click();", element_three);

// To check a Dropbox- And It have a button to list down the values

driver.findElement(By.xpath(".//*[@id='contactdetailpartial-container']/div[@class='elem_left']/div[1]/div/span/span/span[@class='k-select']")).click();
WebElement element_two = driver.findElement(By.xpath("html/body/div[4]/div/ul/li[4]"));
JavascriptExecutor executor_1 = (JavascriptExecutor)driver;
executor_1.executeScript("arguments[0].click();", element_two);


Không có nhận xét nào:

Đăng nhận xét

 
Ky nang ban hang ky-nang-ban-hang
10 10 1125 (c) by
Google Thủ thuật, hacking, tool, code, công cụ