Reputation: 31
Im currently trying to automate a web process Using Selenium and I´m quite new to Java AND Selenium. My main method is Currently Opening a Web page, doing some work on it and then it calls one of my sub methods (which is supposed to work on that same excat webpage instance aswell). Instead the called method opens a new Browser Instance and obviously ends up producing an error. So far i was able to google most of my problems with topics like these, but i cant seem to find a solution for passing a Browser instance over to a called method. Glad for any tipps that you can Provide :-)
package test;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
public classautomationtest {
public static void main(String[] args) {
String projectLocation = System.getProperty("user.dir");
System.setProperty("webdriver.gecko.driver", projectLocation+"\\lib\\Geckodriver\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("some webside"); //didnt put the url in for this post
//.....
//doing loads of stuff on the webside (works perfectly fine)
//.....
//calling for the 2nd methode
methode_two(value1,value2,value3,value4);
}
//this method gets called correctly, but it opens its own browser instance, even though i want it to work on the one the main method did
public static void methode_two(int value1,int value2,int value3,int value4) {
String projectLocation = System.getProperty("user.dir");
System.setProperty("webdriver.gecko.driver", projectLocation+"\\lib\\Geckodriver\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//doing some stuff on the webside
//just a example line:
driver.findElement(By.cssSelector("div.field:nth-child(5) > input:nth-child(1)")).click(); //works like a charm in the main method
}
}
Upvotes: 0
Views: 176
Reputation: 11
You need to pass the webDriver instance as an argument to the function:
package test;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
public classautomationtest {
public static void main(String[] args) {
String projectLocation = System.getProperty("user.dir");
System.setProperty("webdriver.gecko.driver", projectLocation+"\\lib\\Geckodriver\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("some website"); //didn't put the url in for this post
//.....
//doing loads of stuff on the website (works perfectly fine)
//.....
//calling for the 2nd method
method_two(driver, value1, value2, value3, value4);
}
public static void method_two(WebDriver driver, int value1,i nt value2, int value3, int value4) {
String projectLocation = System.getProperty("user.dir");
//doing some stuff on the webside
driver.findElement(By.cssSelector("div.field:nth-child(5) > input:nth-child(1)")).click();
}
Upvotes: 1
Reputation: 1689
Try the below code:
package test;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
public class automationtest {
static WebDriver driver;
public static void main(String[] args) {
String projectLocation = System.getProperty("user.dir");
System.setProperty("webdriver.gecko.driver", projectLocation+"\\lib\\Geckodriver\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
driver.navigate().to("some webside"); //didnt put the url in for this post
//.....
//doing loads of stuff on the webside (works perfectly fine)
//.....
//calling for the 2nd methode
methode_two(value1,value2,value3,value4);
}
//this method gets called correctly, but it opens its own browser instance, even though i want it to work on the one the main method did
public static void methode_two(int value1,int value2,int value3,int value4) {
//doing some stuff on the webside
//just a example line:
driver.findElement(By.cssSelector("div.field:nth-child(5) > input:nth-child(1)")).click(); //works like a charm in the main method
}
}
You need to declare WebDriver outside of the main method so that it can be accessible to other static methods also and initialization will happen in the main() method as it is a starting point for the Java. I hope it helps...
Upvotes: 0