Hi I need to call a method "loop()" 500 times. Do I need to write "loop();" 500 times or is there any method to call it multiple times. Please help with this. The following code is in java and I am doing this with selenium webdriver.
public class Salesforce_login {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:/Users/Master/Desktop/chromedriver.exe");
driver = new ChromeDriver();
// driver = new FirefoxDriver();
Thread.sleep(1000);
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://login.com");
driver.findElement(
By.xpath(".//*[#id='loginSwitcher:loginSwitcherForm']/div[1]/div[1]/div/a"))
.click();
Thread.sleep(1000);
driver.findElement(By.xpath(".//*[#id='username']")).sendKeys(
"*****");
driver.findElement(By.xpath(".//*[#id='password']")).sendKeys(
"*****");
driver.findElement(By.xpath(".//*[#id='Login']")).click();
Thread.sleep(30000);
driver.findElement(By.xpath(".//*[#id='moreGroupMembersLink']"))
.click();
Thread.sleep(1000);
loop();
loop();
loop();
loop();
}
public static void loop() throws InterruptedException{
for (int i = 1; i < 25; i++) {
System.out.println(driver
.findElement(
By.xpath(".//*[#id='groupMembersDialogContent']/div/div[1]/div[2]/div/table/tbody/tr["+i+"]/td[2]/div/a"))
.getAttribute("href"));
}
driver.findElement(By.xpath(".//*[#id='groupMembersDialogContent']/div/div[1]/div[3]/div/span[2]/span[1]/a")).click();
Thread.sleep(2000);
}
}
for(int i = 0; i < 500; i++) {
loop();
}
Related
This is how you can switch browsers pages using linkText in Selenium as below
public class locatorsPractice {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver","/path");
WebDriver driver =new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("https://browser url");
String strMainWindowHandle=driver.getWindowHandle();
System.out.println("Window title"+driver.getTitle());
driver.findElement(By.linkText("Google")).click();
Set <String> strHandles=driver.getWindowHandles();
for (String handle:strHandles) {
driver.switchTo().window(handle);
String strTitle=driver.getTitle();
if(strTitle.equalsIgnoreCase("Google")) {
System.out.println(driver.getTitle());
driver.manage().window().maximize();
Thread.sleep(2000);
driver.close();
}
}
}}
I want to pass my WebDriver to another class instead of passing it to the individual methods within that class. That would mean passing it to the constructor of the class when I create an instance of it. Here is my code, and my issue further below -
public class StepDefinitions{
public static WebDriver driver = null;
CustomWaits waits;
#Before("#setup")
public void setUp() {
driver = utilities.DriverFactory.createDriver(browserType);
System.out.println("# StepDefinitions.setUp(), driver = " + driver);
waits = new CustomWaits(driver);
}
}
public class CustomWaits {
WebDriver driver;
public CustomWaits(WebDriver driver){
this.driver = driver;
}
public boolean explicitWaitMethod(String id) {
boolean status = false;
try {
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
status = element.isDisplayed();
} catch (NullPointerException e){
e.printStackTrace();
}
return status;
}
}
The error I am getting in is NullPointerException when a method of that class is called within an #Given, #When, etc. This is a scope issue I cannot resolve.
Feature File:
#test
Feature: Test
#setup
Scenario: Navigate to Webpage and Assert Page Title
Given I am on the "google" Website
Then page title is "google"
Here is the step definition:
#Given("^element with id \"([^\"]*)\" is displayed$")
public void element_is_displayed(String link) throws Throwable {
if (waits.explicitWaitMethod(link)) {
// This is where waits becomes null when I put a breakpoint
driver.findElement(By.id(link)).isDisplayed();
} else {
System.out.println("Timed out waiting for element to display");
}
}
I would do something like this.
public class StepDefinitions{
public StepDefinitions() {
driver = utilities.DriverFactory.createDriver(browserType);
System.out.println("# StepDefinitions.setUp(), driver = " + driver);
waits = new CustomWaits(driver);
}
public static WebDriver driver = null;
public static CustomWaits waits;
#Given("^element with id \"([^\"]*)\" is displayed$")
public void element_is_displayed(String link) throws Throwable {
if (waits.explicitWaitMethod(link)) {
// This is where waits becomes null when I put a breakpoint
driver.findElement(By.id(link)).isDisplayed();
} else {
System.out.println("Timed out waiting for element to display");
}
}
}
public class CustomWaits {
private static WebDriver driver;
public CustomWaits(WebDriver driver){
this.driver = driver;
}
public boolean explicitWaitMethod(String id) {
boolean status = false;
try {
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
status = element.isDisplayed();
} catch (NullPointerException e){
e.printStackTrace();
}
return status;
}
}
When I run this code with commented //driver = new PhantomJSDriver(desiredCapabilities);, All works fine. I can see only two "PhantomJS" system process.
2 process But if I run this code with uncommented driver = new PhantomJSDriver(desiredCapabilities);, The amount "PhantomJS" system processes more than 2 : more than 2 ... Why?
PhantomJsDriverManager.getInstance().setup();
Semaphore s = new Semaphore(2);
for (int i = 0; i < 10; i++) {
s.acquire();
new Thread() {
public void run() {
PhantomJSDriver driver = new PhantomJSDriver();
try {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
System.out.println("Start: " + getName());
desiredCapabilities.setJavascriptEnabled(true);
driver = new PhantomJSDriver(desiredCapabilities);
driver.get("https://community.oracle.com/");
driver.quit();
System.out.println("Stop: " + getName());
} catch (Exception e) {
driver.quit();
} finally {
driver.quit();
s.release();
}
}
}.start();
}
}
I got the following code:
exch.setOnAction(e -> {
for (int i = 0; i < 10; i++) {
BrowserThread.driver.findElement(By.linkText("Premium Exchange")).click();
String strVal = BrowserThread.driver.findElement(By.id("premium_exchange_stock_wood")).getText();
String strVal2 = BrowserThread.driver.findElement(By.id("premium_points")).getText();
int intVal = Integer.parseInt(strVal);
int intVal2 = Integer.parseInt(strVal2);
if (intVal >= 64 && intVal2 >= 1) {
BrowserThread.driver.findElement(By.name("buy_wood")).clear();
BrowserThread.driver.findElement(By.name("buy_wood")).sendKeys("64"); //enter 64 in the 'buy box'
BrowserThread.driver.findElement(By.xpath("//input[#value='Calculate best offer ']")).click(); //click calculate best offer
BrowserThread.driver.findElement(By.xpath("//div[#id='premium_exchange']/div/div[2]/button")).click(); //click buy
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(BrowserTab.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
LogTab.log.appendText("Not enough premium points.\n");
}
if (stop.isPressed()) {
LogTab.log.appendText("Stopped task.\n");
break;
}
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(MarketTab.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
It basically refreshes a webpage infinitely when a button is pressed on the GUI.
There is a button to start the loop (exch), and a button to break the loop (stop). But my problem is, the GUI freezes when it executes tasks on the browser. This applies to everything, not just this for-loop.
For example, I have a run-button to open the selenium web browser. The GUI freezes until the web browser has loaded completely.
I checked around and found out I have to use threads, but I have no idea where to start. I tried making a separate class for the browser to run on a different thread, but that didn't work and I can't seem to find out what I did wrong.
BrowserThread class:
public class BrowserThread extends Thread {
static WebDriver driver;
private String baseUrl;
private String browsertype;
public BrowserThread(String name, String browsertype) {
super(name);
this.browsertype = browsertype;
}
// set up method to initialize driver object
public void setUp(String browsertype) throws Exception {
if (browsertype.contains("Chrome")) {
System.setProperty("webdriver.chrome.driver","res\\chromedriver.exe");
driver = new ChromeDriver();
} else if (browsertype.contains("PhantomJS")) {
driver = new PhantomJSDriver();
System.setProperty("phantomjs.binary.path", "res\\phantomjs.exe");
} else if (browsertype.contains("PhantomJS Linux")) {
driver = new PhantomJSDriver();
System.setProperty("phantomjs.binary.path", "res/phantomjs.exe");
}
baseUrl = "https://www.google.com/";
driver.get(baseUrl);
}
}
Can any one pls help me out
There are many check boxes available with values and I have to choose a particular value in check box. I dont know how to choose a check box value in selenium webdriver
https://www.blueshieldca.com/fap/app/search.html
This one works.. This will click the check box "General Medicine" under "Type" on left menu...
public class SampleUITest extends SeleneseTestBase {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
try {
driver.get("https://www.blueshieldca.com/fap/app/search.html");
driver.findElement(By.id("location"))
.sendKeys("Locans, Fresno, CA");
driver.findElement(By.className("findNow")).click();
Thread.sleep(1000);
driver.findElement(By.className("continueBtn")).click();
Thread.sleep(15000);
driver.findElement(
By.xpath("//ul[#id='doctortypesmodule']/li[2]/input"))
.click();
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.quit();
}
}
}
This works!!
Your actual issue is with wait
#BeforeTest
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://www.blueshieldca.com/fap/app";
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
#Test
public void Test01() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 60);
driver.get(baseUrl + "/search.html");
driver.findElement(By.xpath("//input[#name='location']")).clear();
driver.findElement(By.xpath("//input[#name='location']")).sendKeys(
"Los Alamitos, Orange, CA");
driver.findElement(By.xpath("//input[#value='findNow']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//input[#onclick='continueallPlans();']"))
.click();
wait.until(ExpectedConditions.elementToBeClickable(By
.xpath("//input[#onclick='javascript:results_OnProviderCompareClicked(this);']")));
List<WebElement> ele = driver
.findElements(By
.xpath("//input[#onclick='javascript:results_OnProviderCompareClicked(this);']"));
System.out.println(ele.size());
ele.get(0).click();
}
Prashanth Sams | seleniumworks.com