I can't connect my BrowserFactory to Selenium Grid. Any ideas why the following code won't work?
public static WebDriver getDriver() throws Exception {
try {
// Load the driver selected by user
Properties p = new Properties();
FileInputStream fi = new FileInputStream(Constant.CONFIG_PROPERTIES_DIRECTORY);
p.load(fi);
if(p.getProperty("use_grid").equalsIgnoreCase("true")) {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.getBrowserName();
desiredCapabilities.setPlatform(Platform.WINDOWS);
return new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), desiredCapabilities);
}
String browserName = p.getProperty("browser");
switch (browserName) {
case "firefox":
if (null == webdriver) {
System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);
webdriver = new FirefoxDriver();
}
break;
I have the node and grid up and running successfully.
Thanks.
It looks like you're not setting the browser when using selenium grid. Try changing this line:
desiredCapabilities.getBrowserName();
to this:
desiredCapabilities.setBrowserName("firefox");
or this if you're properties are in the correct format and that code is working correctly:
desiredCapabilities.setBrowserName(p.getProperty("browser"));
Related
It does not work chrome browser language setting as below using selenium + Java. Can someone help on this to find reson behind that ?
ChromeOptions optionsChrome = new ChromeOptions();
optionsChrome.addArguments("--lang=ja");
driver = new ChromeDriver(optionsChrome);
I think you should call option with setExperimentalOption then add the language.
So it should be like:
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--lang=ja");
I hope this will work for you.
This is my implementation for chrome / firefox
public WebDriver createWebDriver(BrowserType browserType) {
switch (browserType) {
case IE:
return new InternetExplorerDriver();
case CHROME:
if (SystemUtils.IS_OS_WINDOWS) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/chromedriver77.exe");
}
if (SystemUtils.IS_OS_LINUX) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/chromedriver77");
}
return new ChromeDriver();
case FIREFOX:
if (SystemUtils.IS_OS_WINDOWS) {
System.setProperty("webdriver.gecko.driver", "src/test/resources/drivers/geckodriver.exe");
}
if (SystemUtils.IS_OS_LINUX) {
System.setProperty("webdriver.gecko.driver", "src/test/resources/drivers/geckodriver");
}
return new FirefoxDriver();
default:
throw new RuntimeException("Unsupported browserType: " + browserType);
}
I am trying to run Selenium Webdriver script in Chrome, have added following lines in my existing script
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\chromedriver.exe");
private WebDriver driver = new ChromeDriver();
I am building my script in Intellij using Java. Not sure why i am getting "can not resolve symbol setProperty". I tried changing JRE and JDK files but nothing has worked. Any help would be appreciated.
Adding code
public class StartCaseJava extends TestCase {
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
// Getting Date and Timestamp for Last Name
Date d = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MMddyyHHmmss");
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
// private WebDriver driver = new ChromeDriver();
// driver = new FirefoxDriver();
// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
private WebDriver driver = new ChromeDriver();
public void testStartCaseJava() throws Exception {
// System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
It's a long shot but should this
C:\Users\Garimaari\IdeaProjects\Webdriver testing\Chromedriver\chromedriver.exe")
maybe be renamed to C:\Users\Garimaari\IdeaProjects\Webdriver testing\Chromedriver\chromedriver.exe")
or are the double back slashes actually a necessary feature?
This might be because of one missing '\' before chromedriver.exe in your setProperty string.
Try using :
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
I am able to run my script using Chrome now. Here is the small sample declaration I used:
class StartCaseJAva {
static WebDriver driver;
public void testcasejava()) {
System.setProperty(path);
driver = new ChromeDriver();
}
}
I'm a rookie test developer using selenium 2.45 and I'm trying to configure my FirefoxDriver to use my company's proxy settings. I am failing to do so :)
I am following the instruction from here to create a profile on-the-fly:
Using a Proxy for FF
My code looks like this:
public static WebDriver driver;
String usedProxy = "http://myproxy:8080";
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(usedProxy).setFtpProxy(usedProxy).setSslProxy(usedProxy);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
driver = new FirefoxDriver(cap);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://TestWebsite.com");
I am not receiving any kind of errors but the connection is not working for this browser. When checking Options>Advanced>Network>Connection Settings from Firefox menu, proxy is set to manual but text inputs only contain "http://"
PS: I have a feeling this is relevant but am not sure: TestWebsite.com will load via https only (it's a shopping cart)
If your goal is to test your functionality on different IP addresses, you can use the Tor Browser.
public IWebDriver Driver { get; set; }
public Process TorProcess { get; set; }
public WebDriverWait Wait { get; set; }
[TestInitialize]
public void SetupTest()
{
String torBinaryPath = #"C:\Users\aangelov\Desktop\Tor Browser\Browser\firefox.exe";
this.TorProcess = new Process();
this.TorProcess.StartInfo.FileName = torBinaryPath;
this.TorProcess.StartInfo.Arguments = "-n";
this.TorProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
this.TorProcess.Start();
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.socks", "127.0.0.1");
profile.SetPreference("network.proxy.socks_port", 9150);
this.Driver = new FirefoxDriver(profile);
this.Wait = new WebDriverWait(this.Driver, TimeSpan.FromSeconds(60));
}
[TestCleanup]
public void TeardownTest()
{
this.Driver.Quit();
this.TorProcess.Kill();
}
Here is the code to refresh the Tor identity.
public void RefreshTorIdentity()
{
Socket server = null;
try
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9151);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Connect(ip);
server.Send(Encoding.ASCII.GetBytes("AUTHENTICATE \"johnsmith\"" + Environment.NewLine));
byte[] data = new byte[1024];
int receivedDataLength = server.Receive(data);
string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
server.Send(Encoding.ASCII.GetBytes("SIGNAL NEWNYM" + Environment.NewLine));
data = new byte[1024];
receivedDataLength = server.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
if (!stringData.Contains("250"))
{
Console.WriteLine("Unable to signal new user to server.");
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
finally
{
server.Close();
}
}
You can find more detailed information here: http://automatetheplanet.com/using-selenium-webdriver-tor-c-code/
The code examples are in C# but the code should be identical in Java.
Try firefox profile as well. Note this is C# code and converting to Java should be fairly simply
string usedProxy = "http://myproxy:8080";
Proxy proxy = new OpenQA.Selenium.Proxy();
proxy.HttpProxy = usedProxy;
proxy.FtpProxy = usedProxy;
proxy.SslProxy = usedProxy;
FirefoxProfile profile = new FirefoxProfile();
profile.SetProxyPreferences(proxy);
This code works for me:
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", <proxy_url>);
profile.setPreference("network.proxy.http_port", 8080);
profile.setPreference("network.proxy.ssl", <proxy_url>);
profile.setPreference("network.proxy.ssl_port", 8080);
options.setProfile(profile);
options.setAcceptInsecureCerts(true)
options.setCapability("disable-restore-session-state", true);
options.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(options);
I followed answers from this link: Webdriver and proxy server for firefox
So I am looking to do some simple data collection on a few .onion sites. I am going about this by using selenium webdriver to call Tor as part of the Firefox webdriver. However, I can't seem to figure out how to get firefox to successfully go to .onion sites. Here is the code.
public static void main(String[] args) throws InterruptedException, IOException {
File torProfileDir = new File("C:\\Users\\Chambers\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default");
FirefoxBinary binary = new FirefoxBinary(new File("C:\\Users\\Chambers\\Desktop\\Tor Browser\\Browser\\firefox.exe"));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
torProfile.setPreference("webdriver.load.strategy", "unstable");
try {
binary.startProfile(torProfile, torProfileDir, "");
} catch (IOException e) {
e.printStackTrace();
}
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.socks", "127.0.0.1");
profile.setPreference("network.proxy.socks_port", 9150);
FirefoxDriver driver = new FirefoxDriver(profile);
int firstCheck = "0";
while (firstCheck == 0) {
driver.navigate().to("onion site here");
......
The problem I am having is that I end up with a firefox browser that cannot connect to .onion sites. If I change FirefoxDriver driver = new FirefoxDriver(profile); to FirefoxDriver driver = new FirefoxDriver(binary, profile); then I am left with a blank Tor window that I can't seem to control with the webdriver.
Anyone have any ideas on how to fix this? any help would be appreciated!
Figured it out. Needed to add a lot of permissions to the new profile. Here is the fixed code for those that are interested. It allows you to browse the dark web with Firefox as if you were using Tor. The commands for controlling the webdriver don't change.
File torProfileDir = new File("C:\\Users\\Chambers\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default");
FirefoxBinary binary = new FirefoxBinary(new File("C:\\Users\\Chambers\\Desktop\\Tor Browser\\Browser\\firefox.exe"));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
torProfile.setPreference("webdriver.load.strategy", "unstable");
try {
binary.startProfile(torProfile, torProfileDir, "");
} catch (IOException e) {
e.printStackTrace();
}
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.socks", "127.0.0.1");
profile.setPreference("network.proxy.socks_port", 9150);
profile.setPreference("network.proxy.socks_version", 5);
profile.setPreference("places.history.enabled", false);
profile.setPreference("privacy.clearOnShutdown.offlineApps", true);
profile.setPreference("privacy.clearOnShutdown.passwords", true);
profile.setPreference("privacy.clearOnShutdown.siteSettings", true);
profile.setPreference("privacy.sanitize.sanitizeOnShutdown", true);
profile.setPreference("signon.rememberSignons", false);
profile.setPreference("network.cookie.lifetimePolicy", 2);
profile.setPreference("network.dns.disablePrefetch", true);
profile.setPreference("network.http.sendRefererHeader", 0);
profile.setPreference("network.proxy.socks_remote_dns", true);
FirefoxDriver driver = new FirefoxDriver(profile);
String firstCheck = "";
while (firstCheck == 0) {
driver.get("http://kbhpodhnfxl3clb4.onion/");
.........................
public class FooTest {
WebDriver driver;
#Before
public void beforeTest() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new PhantomJSDriver(capabilities);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
}
#Test
public void test() {
driver.get("http://www.example.com");
WebElement e = driver.findElement(By.tagName("h1"));
System.out.println("TEXT" + e.getAttribute("innerHTML"));
assertNotNull(e);
driver.quit();
}
}
Hi, I'm just simply trying get the h1 tag in www.example.com which says "Example Domain." The code works for http://www.example.com but not for https://www.exmaple.com. How can I fix this problem? Thanks
PhantomJSDriver does not support (all) DesiredCapabilities.
You will need:
caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes"});
driver = new PhantomJSDriver(caps);
Documented here: https://github.com/detro/ghostdriver/issues/233