My problem is Firefox. I installed a different location. But I tried this solution isn't working for me.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxProfile;
import java.io.File;
public class firefoxDriver
{
public static void main(String[] args)
{
File pathBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary ffBinary = new FirefoxBinary(pathBinary);
FirefoxProfile ffProfile = new FirefoxProfile();
// Add .exe file
System.setProperty("webdriver.gecko.driver", "D:\\Docs\\Drivers\\geckodriver.exe");
// Create Firefox object driver.
WebDriver ffDriver = new FirefoxDriver(ffBinary, ffProfile);
ffDriver.get("https://google.com");
System.out.println(ffDriver.getTitle());
}
}
The error I get:
N.B - In your particular example you do not need to create a profile. New profile is created automatically by default. You use profiles when you have some pre-configured profiles persisted before you run your driver.
You can do this:
FirefoxOptions options = new FirefoxOptions();
options.setBinary(new FirefoxBinary(new File("...")));
options.setProfile(new FirefoxProfile(new File("...")));
WebDriver = new FirefoxDriver(options);
Another way to specify browser binary isto use system property:
System.setProperty(
FirefoxDriver
.SystemProperty.BROWSER_BINARY, "PATH_TO_YOUR_BINARY");
If you have a named profile set up for your instance of firefox browser, then you should do the following (like in previous case before you create a driver):
System.setProperty(
FirefoxDriver
.SystemProperty.BROWSER_PROFILE, "NAME_OF_PROFILE");
There is not constructor that is overloaded to have binary and profile together. You should use options.
Code :
System.setProperty("webdriver.gecko.driver", "D:\\Docs\\Drivers\\geckodriver.exe");
String pathBinary = "D:\\Program Files\\Mozilla Firefox\\firefox.exe";
FirefoxOptions ffOptions = new FirefoxOptions();
FirefoxProfile ffProfile = new FirefoxProfile(pathBinary);
ffOptions.setProfile(ffProfile);
ffOptions.setBinary("C:\\Program Files\\Mozilla Firefox 52\\firefox.exe");
// Create Firefox object driver.
WebDriver ffDriver = new FirefoxDriver(ffOptions);
ffDriver.get("https://google.com");
System.out.println(ffDriver.getTitle());
public static void main(String[] args)
{
// Add .exe file
System.setProperty("webdriver.gecko.driver", "D:\\Docs\\Drivers\\geckodriver.exe");
File pathBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary ffBinary = new FirefoxBinary(pathBinary);
DesiredCapabilities desired = DesiredCapabilities.firefox();
FirefoxOptions ffOptions = new FirefoxOptions();
desired.setCapability(FirefoxOptions.FIREFOX_OPTIONS, ffOptions.setBinary(ffBinary));
// Create Firefox object driver.
WebDriver ffDriver = new FirefoxDriver(ffOptions);
ffDriver.get("https://google.com");
System.out.println(ffDriver.getTitle());
}
I solved my problem with this method. I don't know there is a much more simple solution.
Related
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
public class torr1 {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.marionette",
"C:\\Users\\ghorh\\Documents\\selenium-bazinga\\Drivers\\geckodriver.exe");
String torPath = "C:\\Users\\ghorh\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "C:\\Users\\ghorh\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
File torProfileDir = new File(profilePath);
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
options.setProfile(torProfile);
options.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);
WebDriver driver = new FirefoxDriver(options);
driver.get("http://google.co.in");
}
}
I have tried the above code so far but getting message: 'tor failed to start'. Could somebody please help on what changes are required for the above code.
You are trying to use FireFox driver.
Try use TorBrowserDriver as specified in its readme: https://github.com/webfp/tor-browser-selenium
Or if you want to use firefox - use GeckoDriver that can be downloaded from the next link:
https://github.com/mozilla/geckodriver/releases/tag/v0.26.0
I am trying to open local files with Selenium. With the code below, Firefox is opening, but I have the error org.openqa.selenium.WebDriverException: Timed out waiting 45 seconds for Firefox to start..
File gecko = new File("resources/geckodriver64.exe");
System.setProperty("webdriver.gecko.driver", gecko.getAbsolutePath());
FirefoxOptions capabilities = new FirefoxOptions();
capabilities.setCapability("marionette", false);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("file:///C:/example/myfile.pdf");
Can someone help me ? I couldn't find anything on the internet.
We have now come to the part where you will see how you can use GeckoDriver to launch Firefox. You will first need to download GeckoDriver and then set its path. There are three different ways to use GeckoDriver with Selenium 3:
With setting system properties in the test
With setting system properties by Environment Variable
With setting up Browser Desired Capabilities
Download Gecko Driver:-
1- Gecko Driver different versions can be downloaded from Github. I suggest you to use the latest version.
Set System Properties for Gecko Driver:-
Code to set the System properties is System.setProperty(“webdriver.gecko.driver”,”Path to geckodriver.exe”);
The complete program to launch the GeckoDriver will be like this:
package seleniumPrograms;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Gecko_Driver {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "D:\\\\XXXX\\trunk\\Library\\drivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com");
Thread.sleep(5000);
driver.quit();
}
}
Check Below answer. This is working solution on my machine. Please check your firefox version too.
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class geckodriver {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\username\\Downloads\\geckodriver-v0.20.1-win64\\geckodriver.exe");
Thread.sleep(5000);
// DesiredCapabilities capabilities = DesiredCapabilities.firefox();
// capabilities.setCapability("marionette", true);
//
// WebDriver driver = new FirefoxDriver(capabilities);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.firefox();
capabilities.setBrowserName("firefox");
capabilities.setVersion("your firefox version");
capabilities.setPlatform(Platform.WINDOWS);
capabilities.setCapability("marionette", false);
WebDriver driver = new FireFoxDriver(capabilities);
driver.get("http://www.google.com");
Thread.sleep(5000);
driver.quit();
}}
Can you try below code ?
package seleniumPrograms;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Gecko_Driver {
public static void main(String[] args) throws InterruptedException {
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.google.com");
Thread.sleep(5000);
driver.quit();
}
Unable to compile code line "options.addarguments("--start-maximized") ", using selinum 3.0.1 and using ChromDriver_win32 latest version and eclispe Mars.. Let me know what i am missing. I am able to compile and run my test without options..
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
public class IRSLabTestCase {
WebDriver driver1= new ChromeDriver();
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized"); //--> this line not getting compiled.
driver1 = new ChromeDriver(options);'
}
It wont work because you can only initialize members in a class.
To do operations on them you have to put it in a function or do those in a constructor.
You are initializing driver1 object twice .You have to learn java basics.Else you can't proceed further.
You can do like below.
public class IRSLabTestCase {
WebDriver driver1;
ChromeOptions options = new ChromeOptions();
public IRSLabTestCase(){
options.addArguments("--start-maximized");
driver1 = new ChromeDriver(options);'
}
}
Instead of chrome options you should try this:
ChromeDriver driver;
driver=new ChromeDriver();
driver.manage().window().maximize();
or
ChromeOptionsoptions =new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
When I am trying to instantiate AndroidDriver class it is giving an error. Please help.
Code
import io.appium.java_client.android.AndroidDriver;
public class Testing {
#Test
public void testMethod() {
AndroidDriver driver;
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "samsung-sm_g530h-5554c610");
cap.setCapability("platformVersion", "4.4.4");
cap.setCapability("platformName", "Android");
cap.setCapability(CapabilityType.BROWSER_NAME, "");
cap.setCapability("appPackage", "com.whatsapp");
cap.setCapability("appActivity", "com.whatsapp.HomeActivity");
driver = new AndroidDriver(new URL("127.0.0.1:4723"), cap);
}
}
// Here is the error
It is giving an error: AndroidDriver is Raw type. You can initialize driver as below:
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
...
public class Testing()
{
public AppiumDriver driver;
...
#BeforeTest
public void testMethod()
{
driver = new AndroidDriver(new URL(Node), capabilities);
...
}
}
I also met this problem before, but I now have solved, and the reasons for this problem is because Java - client-(version number). jar is not compatible,So I will Java - client-(version number). jar replacement into Java - the client - 3.1.0. Jar.Hope to be able to help you!
Try replacing:
driver = new AndroidDriver(new URL("127.0.0.1:4723"), cap);
With:
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
You are getting this error as AppiumDriver is now Generic, so it can be set to return elements of class MobileElement or IOSElement or AndroidElement without casting.
This change is introduced in Java client version 3.0 and above. Details can be found here
Also, app package, app activity and device name is sufficient enough to run tests. So, you can modify your code as:
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
public class Testing {
AndroidDriver<MobileElement> driver;
#Test
public void testMethod() {
DesiredCapabilities caps = new DesiredCapabilities() ;
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"samsung-sm_g530h-5554c610");
caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.whatsapp");
caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.whatsapp.HomeActivity");
driver = new AndroidDriver<MobileElement>(new URL ("http://127.0.0.1:4723/wd/hub"),caps);
}
}
Following is the correct way to initialize Androidriver:
public class AppiumController{
public static void main(String[] args) throws MalformedURLException{
AppiumDriver<?> driver;
final String URL_STRING = "http://127.0.0.1:4723/wd/hub";
URL url = new URL(URL_STRING);
File appDirAndroid = new File("src/main/resources/app/");
File appAndroid = new File(appDirAndroid, "in.amazon.mShop.android.shopping_2018-02-22.apk");
DesiredCapabilities androidCapabilities = new DesiredCapabilities();
androidCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
androidCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
androidCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
androidCapabilities.setCapability("appPackage", "in.amazon.mShop.android.shopping");
androidCapabilities.setCapability("appActivity", "com.amazon.mShop.home.HomeActivity");
androidCapabilities.setCapability(MobileCapabilityType.APP, appAndroid.getAbsolutePath());
driver = new AndroidDriver<MobileElement>(url, androidCapabilities);
driver.closeApp();
}
}
The above piece of code will successfully launch the amazon app on emulator.
In order to take screen shots we have to augment driver with screen shot feature in following way:
driver = new Augmenter().augment(driver);
But when we run this code in Chrome it opens new empty window. How can I avoid it?
Here is my code:
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.Augmenter;
public class NewChromeTest {
public static void main(String args[]) throws IOException {
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com.ua/");
driver = new Augmenter().augment( driver );
}
}
What steps will reproduce the problem?
1. Run code above
What is the expected output?
We should stay in the same browser window.
What do you see instead?
New empty browser (Chrome) window opens
Selenium version: 2.37
OS: Win 7
Browser: Chrome
Browser version: 31.0.1650.63 m!
screenshot
Try adding the below two lines to your code
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("D:\\screenshot.png"));
Try this.
driver= new Augmenter().augment(driver);
File scrFile =(File) ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(directory.getCanonicalFile()+"\\Images\\LatestFailedScreenshot.png"));