I am trying to launch Chrome with a specific Homepage set. Given below is the code, I am using:
package WebDriverInitialization;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class LaunchChrome {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\Technology Lab\\+ProgramFiles\\selenium-drivers\\chromedriver.exe");
Map<String, Object> hmPrefs = new HashMap<String, Object>();
hmPrefs.put( "browser.startup.page", 1);
hmPrefs.put( "browser.startup.homepage", "http://www.seleniumhq.org");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", hmPrefs);
DesiredCapabilities chromeCaps = DesiredCapabilities.chrome();
chromeCaps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver chromeDriver = new ChromeDriver(chromeCaps);
chromeDriver.manage().window().maximize();
}
}
When I run this, I get a blank page with 'data:,' in the URL - like how Chrome launches by default. Last line of the code is getting executed and the page is maximized.
I am using Selenium version 3.0.1; java version 1.8.0_92; Chrome version 56.0.2924.87 and ChromeDriver version 2.27.440174 on Windows 7 Professional SP1 x64.
Can anyone point out the mistake in above code and get it to launch Chrome with http://www.seleniumhq.org as the homepage?
Thanks!
Try this:
chromeOptions.setArguments("google-base-url=MY_URL");
From doc: Define kGoogleBaseURL
Specifies an alternate URL to use for speaking to Google. Useful for testing.
Related
I am trying to run automated test with appium, but I always get the following error :
FAILED: f
java.lang.NoSuchMethodError: org.openqa.selenium.remote.http.HttpClient$Factory.createDefault()Lorg/openqa/selenium/remote/http/HttpClient$Factory;
I've noticed that if I delete everything from the driver declaration to the bottom, the test is successful. Here is my code :
package test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
public class FirstTest {
#Test
public void f() throws MalformedURLException, InterruptedException {
new DesiredCapabilities();
//Set the Desired Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.BROWSER_NAME, BrowserType.CHROME);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME , Platform.ANDROID);
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "My Phone");
caps.setCapability("udid", "K6T6R16C01001259"); //Give Device ID of your mobile phone
caps.setCapability("platformName", "Android");
caps.setCapability(MobileCapabilityType.VERSION, "7.0");
caps.setCapability("appPackage", "com.android.chrome");
caps.setCapability("appActivity", "com.google.android.apps.chrome.Main");
caps.setCapability("noReset", "true");
//Instantiate Appium Driver
AppiumDriver<MobileElement> driver = null;
try {
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
driver.get("http://www.google.com");
}
}
Solved it on my own. I converted the project into maven one and it works like a charm now.
I had the the same problem on a dual appium + selenium project, which had been working fine.
It stopped to work with the same exception the day I upgraded intellij...
Somehow a duplicate dependency was inserted into the pom.xml.
(existing org.seleniumhq.selenium in 3.141.59 and 3.6. version), of which I was not aware, which I suspect was introduced by the install and not by some missed move of me.
So the source of the error was coming from this duplicate org.seleniumhq.selenium 3.6.
(not compatible with either Appium 7.0.2 or com.squareup.okhttp3 or both...).
Summary :
remove the org.seleniumhq.selenium 3.6 duplicate dependency,
empty the "target" directory
run a "maven clean" task.
Then all was back up to work.
so that appium 7.0.2, selenium 3.141.59 and com.squareup.okhttp3 3.11.0 were all doing fine together again.
I tried the below code with actual credentials for Indeed.com,
and I get an error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //*[#id="signin_email"]
I get a similar error when I use By.id instead of By.xpath, any idea what's going on?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Testing {
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver();
driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
driver.findElement(By.xpath("//*[#id=\"signin_email\"]")).sendKeys("notworking#gmail.com");
driver.findElement(By.xpath("//*[#id=\"signin_password\"]")).sendKeys("needHelp");
driver.findElement(By.xpath("//*[#id=\"loginform\"]/button")).click();
driver.quit();
}
}
You need to enable java script, see the updated code.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Testing {
public static void main(String[] args) {
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
driver.findElement(By.xpath("//*[#id=\"signin_email\"]")).sendKeys("notworking#gmail.com");
driver.findElement(By.xpath("//*[#id=\"signin_password\"]")).sendKeys("needHelp");
driver.findElement(By.xpath("//*[#id=\"loginform\"]/button")).click();
driver.quit();
}
}
The login form is added via JS not regular HTML. I tried to disable JS in the browser and all I could see was:
<div id="container"></div>
This means that all you have to do is:
Enable JavaScript for your driver: driver.setJavascriptEnabled(true);
Wait until "signin_email" is rendered: driver.until(ExpectedConditions.presenceOfElementLocated(By.id("signin_email")))
The second bullet will give you stable test on different PCs. Sometimes, on weaker machines, the assertions may execute faster than the element was rendered by JS which leads to random failures.
I am using selenium 3.4.0 , firefox version 53.0 and gecko driver 0.16.1 , java compiler 1.7.
For some sites insecure connection error is displayed.
I have used firefox profile object as follow but still it's not resolving:
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", 3128);
WebDriver driver = new FirefoxDriver(profile);
driver.manage().window().maximize();
To work with Selenium 3.4.0 with Mozilla Firefox browser 53.x you need to download the latest geckodriver from here. Save it in your machine & provide the absolute path of the geckodriver.
Create a new Firefox profile manually by the name debanjan and use the AcceptUntrustedCertificates & setAssumeUntrustedCertificateIssuer options.
This code executes fine with some simple tweak to your own code.
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
testprofile.setAcceptUntrustedCertificates(true);
testprofile.setAssumeUntrustedCertificateIssuer(true);
testprofile.setPreference("network.proxy.type", 1);
testprofile.setPreference("network.proxy.http", "localhost");
testprofile.setPreference("network.proxy.http_port", 3128);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testprofile);
dc.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(dc);
Let me know if this helps you.
Try using firefox below 48 version. You won't face any problem or include below code in your existing code:
System.setProperty("webdriver.firefox.bin" ,"C:/Users/siddhesh.kalgaonkar/AppData/Local/Mozilla Firefox/firefox.exe");
It should solve your problem because this is what I use for current firefox version.
Use Firefox 54.0 64bit, Selenium v3.4.0, jcommender v1.7, TestNG v6.9.9, Java v8.0, Gecko driver v0.17.0
Use below code-
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
public class AppUrl {
public static WebDriver driver;
public static final String url = "https://10.10.1.1";
#BeforeTest
public void setup() throws Exception {
System.setProperty("webdriver.gecko.driver","C:/Users/Downloads/geckodriver.exe");
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setAcceptInsecureCerts(true);
driver = new FirefoxDriver(desiredCapabilities);
driver.get(url);
}
}
Even I tried other sample codes from different sites. Today after I upgrade all the softwares and run the code, it worked for me.
I am receiving the following errors when I am trying to run my script
org.testng.TestNGException: Cannot instantiate class Caused by:
java.lang.reflect.InvocationTargetException Caused by:
java.lang.IllegalStateException: The path to the driver executable
must be set by the webdriver.ie.driver system property;
package EDRTermsPackge;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
public class ContactInformationTesting {
//For use of IE only; Please enable for IE Browser
WebDriver driver = new InternetExplorerDriver();
#BeforeMethod
public void beforeMethod() {
//Using or Launching Internet Explorer
String exePath = "\\Users\\jj85274\\Desktop\\IEDriverServer.exe";
//For use of IE only; Please enable for IE Browser
System.setProperty("webdriver.ie.driver", exePath);
}
#Test
public void OpenPage_Login() {
driver.get("http://cp-qa.harriscomputer.com/");
}
}
You should set your path to driver first and then instantiate IEDriver, you can't use new InternetExplorerDriver(); before System.setProperty("webdriver.ie.driver", exePath);
In your case you could do something like this (No need for #BeforeMethod to do just this simple property setup):
public class ContactInformationTesting {
//Using or Launching Internet Explorer
String exePath = "\\Users\\jj85274\\Desktop\\IEDriverServer.exe";
//For use of IE only; Please enable for IE Browser
System.setProperty("webdriver.ie.driver", exePath);
//For use of IE only; Please enable for IE Browser
WebDriver driver = new InternetExplorerDriver();
#Test
public void OpenPage_Login() {
driver.get("http://cp-qa.harriscomputer.com/");
}
String exePath = "\Users\jj85274\Desktop\IEDriverServer.exe";
This line kind of hints as if you are trying to set the IEDriverServer binary from a network drive. Is that so ? I am not sure if network paths can be accessed by Java code directly.
I would suggest that instead of trying to add the IEDriverServer.exe path in your source code for every test, you might as well include this binary in your PATH variable. You can do this on windows by dropping this exe into one of the directories that is listed in the output of the below command
On Windows
echo %PATH%
On Non-Windows
echo $PATH
I started using selenide (selenium wrapper api) and must say its a great tool but my only issue is its lack of documentation or usage examples online yet.
Any idea how to run your application coded in selenide in google-Chrome. I am using eclipse as IDE. I have added an environment variable "browser" with value chrome in my run configuration but when I run it picks up firefox.
My stack is
JDBC
Java
Selenide
Try this
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
System.setProperty("selenide.browser", "Chrome");
open("http://google.com");
You can find some documentation here.
The below code will help you launch the Chrome Browser with Selenide, and not with the selenium. It means you don't have to issue a close or quit command at the end of the iteration.
import static com.codeborne.selenide.CollectionCondition.size;
import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.*;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.By;
import com.codeborne.selenide.junit.ScreenShooter;
public class SimpleDateFormatClass {
#Rule
public ScreenShooter takeScreenshotSelenide = ScreenShooter.failedTests().succeededTests();
#Test
public void checkGoogleSearchResultsReturnValidResultNumberAndText() {
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver_2");
//Doesn't matter chrome or Chrome as this is case insensitive.
System.setProperty("selenide.browser", "Chrome");
open("http://google.com");
$(By.name("q")).setValue("Selenide").pressEnter();
// assure there are 10 results in the page
// static import shortcut, place the cursor on the method and press
// ctrl+shift+m/ cmd+shift+m
// $ -> driver.findElement, $$ -> driver.findElements
$$(".iris li.g").shouldHave(size(10));
$(".iris li.g").shouldHave(text("Selenide World!"));
}
}
This should help you even if you are running from the command prompt/ terminal, but if you want to exclusively pass on the Chrome from cmd you can use the "browser" parameter as below
-Dselenide.browser=chrome
You need to tell Selenide what browser to use. That can be done using Configuration properties:
import com.codeborne.selenide.Configuration;
public class Tests {
#Before
public void setBrowser() {
Configuration.browser = "chrome";
}
Remember: your webdriver should be placed on the standard path. For unix/linux it is: /usr/local/bin;
If your webdriver is located on a different path or renamed -- you need to set a system property with right path to the webdriver. For example:
Windows:
System.setProperty("webdriver.chrome.driver", "C:\\Program files\\chromedriver.exe");
Linux\Unix:
System.setProperty("webdriver.chrome.driver","/usr/share/chromedriver");
Make sure that your unix / linux chromedriver is executable. After this, you should have a fully working example (in my case, chromedriver is renamed and has version information):
import com.codeborne.selenide.*;
import org.openqa.selenium.*;
import org.junit.*;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.WebDriverRunner.getWebDriver;
public class TestClass {
#Before
public void setBrowser(){
Configuration.browser = "chrome";
Configuration.browserSize = "1920x1080";
Configuration.holdBrowserOpen = true;
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver_2.33");
}
#Test
public void gotoGoogle(){
open("https://www.google.com");
WebElement searchBox = $(By.xpath("//input[#id='lst-ib']"));
$(searchBox).shouldBe(Condition.visible).setValue("How can I execute Selenide in Chrome using ChromeDriver").pressEnter();
WebElement firstResultLink = $(By.xpath("(//div[#class='rc']//a)[1]"));
$(firstResultLink).click();
System.out.println(getWebDriver().getCurrentUrl());
}
}
Another way is to use this command line switch with Maven:
mvn test -P chrome
It requires the Maven profiles in the pom.xml file such as are seen here:
https://github.com/selenide-examples/google
You can use System.setProperty("selenide.browser", "chrome"); for running in the chrome browser. If the same test you need to perform in safari just change the chrome to safari.
Eg:
System.setProperty("selenide.browser", "safari"); open("http://idemo.bspb.ru/");