Last active
November 15, 2020 05:30
-
-
Save ageekymonk/0476551f27c31b1763ef to your computer and use it in GitHub Desktop.
Selenium Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<WebElement> options = driver.findElements(By.cssSelector("xxx")); | |
for (Iterator<WebElement> iterator = options.iterator(); iterator.hasNext();) { | |
WebElement webElement = (WebElement) iterator.next(); | |
try | |
{ | |
if (webElement.findElement(By.cssSelector("k-item")).getText().equals("Processing")) | |
{ | |
webElement.findElement(By.cssSelector("k-item")).click(); | |
webElement.findElement(By.cssSelector("k-icon")).click(); | |
} | |
} | |
catch (NoSuchElementException e){} | |
} | |
} | |
Filename xxx.java | |
public class xxx { | |
public static void main(String[] args) throws Exception { | |
} | |
} | |
-- default webdriver | |
import org.openqa.selenium.*; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.support.ui.Select; | |
public class seleniumTests { | |
public static void main(String[] args) throws Exception { | |
WebDriver driver; | |
String baseUrl; | |
driver = new FirefoxDriver(); | |
baseUrl = "http://www.highcharts.com/demo"; | |
driver.get(baseUrl); | |
// Business Logic here | |
driver.quit(); | |
} | |
} | |
-- multiple test case webdriver | |
import java.util.Iterator; | |
import java.util.List; | |
import org.openqa.selenium.*; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.support.ui.Select; | |
public class seleniumTests { | |
public void Test1() | |
{ | |
WebDriver driver; | |
String baseUrl; | |
driver = new FirefoxDriver(); | |
baseUrl = "http://www.highcharts.com"; | |
driver.get(baseUrl+"/demo"); | |
try | |
{ | |
driver.wait(5000); | |
} | |
catch (Exception e) | |
{ | |
} | |
driver.quit(); | |
} | |
public void Test2() | |
{ | |
} | |
public static void main(String[] args) throws Exception | |
{ | |
seleniumTests mytestsuite = new seleniumTests(); | |
mytestsuite.Test1(); | |
mytestsuite.Test2(); | |
} | |
} | |
----- Iterator loop | |
import java.util.Iterator; | |
import java.util.List; | |
import org.openqa.selenium.*; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.support.ui.Select; | |
public class seleniumTests { | |
public void Test1() | |
{ | |
WebDriver driver; | |
String baseUrl; | |
driver = new FirefoxDriver(); | |
baseUrl = "http://www.highcharts.com"; | |
driver.get(baseUrl+"/demo"); | |
WebElement a = driver.findElement(By.id("container")); | |
WebElement a = driver.findElement(By.cssSelector("#containter")); | |
List<WebElement> options = driver.findElements(By.cssSelector("div")); | |
for (Iterator<WebElement> iter = options.iterator(); iter.hasNext();) | |
{ | |
WebElement abc = (WebElement) iter.next(); | |
List<WebElement> script_options = abc.findElements(By.cssSelector("script")); | |
for (Iterator<WebElement> script_iterator = script_options.iterator(); script_iterator.hasNext();) | |
{ | |
WebElement xyz = (WebElement) script_iterator.next(); | |
} | |
} | |
try | |
{ | |
driver.wait(5000); | |
} | |
catch (Exception e) | |
{ | |
} | |
driver.quit(); | |
} | |
public void Test2() | |
{ | |
} | |
public static void main(String[] args) throws Exception | |
{ | |
seleniumTests mytestsuite = new seleniumTests(); | |
mytestsuite.Test1(); | |
mytestsuite.Test2(); | |
} | |
} | |
--------- wait until --------- | |
import java.util.Iterator; | |
import java.util.List; | |
import org.openqa.selenium.*; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.support.ui.ExpectedConditions; | |
import org.openqa.selenium.support.ui.Select; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
public class seleniumTests { | |
public void Test1() | |
{ | |
WebDriver driver; | |
String baseUrl; | |
driver = new FirefoxDriver(); | |
baseUrl = "http://www.highcharts.com"; | |
driver.get(baseUrl+"/demo/pie-basic"); | |
WebElement a = driver.findElement(By.cssSelector("g.highcharts-series-group g")); | |
List<WebElement> options = a.findElements(By.cssSelector("path")); | |
// options.get(1).click(); | |
for (Iterator<WebElement> iter = options.iterator(); iter.hasNext();) | |
{ | |
WebElement abc = (WebElement) iter.next(); | |
if (abc.getAttribute("fill").equals("#7cb5ec")) | |
{ | |
abc.click(); | |
WebElement myDynamicElement = (new WebDriverWait(driver, 10)) | |
.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement"))); | |
} | |
} | |
// driver.quit(); | |
} | |
--- | |
A sample code for opening and searching in google | |
import java.util.Iterator; | |
import java.util.List; | |
import org.openqa.selenium.*; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.ie.InternetExplorerDriver; | |
import org.openqa.selenium.support.ui.Select; | |
public class seleniumTests { | |
public static void main(String[] args) throws Exception { | |
WebDriver driver; | |
String baseUrl; | |
driver = new FirefoxDriver(); | |
baseUrl = "https://www.google.com.au/"; | |
driver.get(baseUrl + "/?gfe_rd=cr&ei=YKePU_-MFcTC8gfi44D4BQ&gws_rd=ssl"); | |
driver.findElement(By.id("gbqfq")).clear(); | |
driver.findElement(By.id("gbqfq")).sendKeys("Hello world"); | |
driver.wait(); | |
driver.quit(); | |
} | |
} | |
A sample code for testing highcharts and kendoUI | |
import java.util.Iterator; | |
import java.util.List; | |
import org.openqa.selenium.*; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.ie.InternetExplorerDriver; | |
import org.openqa.selenium.support.ui.Select; | |
public class Test2 { | |
private WebDriver driver; | |
private String baseUrl; | |
public void test1() throws Exception { | |
driver = new FirefoxDriver(); | |
driver.get("file:///E:/telerik.kendoui.professional.2014.1.528.trial/index.html"); | |
WebElement firstDropDown = driver.findElement(By.cssSelector("span.k-widget")); | |
firstDropDown.click(); | |
List<WebElement> options = driver.findElements(By.cssSelector("div.k-list-container ul.k-list li.k-item")); | |
for (Iterator<WebElement> iterator = options.iterator(); iterator.hasNext();) { | |
WebElement webElement = (WebElement) iterator.next(); | |
if (webElement.getText().equals("Orange")) | |
webElement.click(); | |
} | |
driver.wait(5000); | |
driver.quit(); | |
} | |
public void test2() throws Exception { | |
driver = new FirefoxDriver(); | |
driver.get("http://www.highcharts.com/demo/pie-basic"); | |
List<WebElement> options = driver.findElements(By.cssSelector("g")); | |
for (Iterator<WebElement> iterator = options.iterator(); iterator.hasNext();) { | |
WebElement webElement = (WebElement) iterator.next(); | |
try | |
{ | |
if (webElement.findElement(By.cssSelector("text tspan")).getText().equals("IE")) | |
webElement.click(); | |
} | |
catch (NoSuchElementException e){} | |
} | |
driver.wait(5000); | |
driver.quit(); | |
} | |
public static void main(String[] args) throws Exception { | |
Test2 a = new Test2(); | |
a.test2(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment