I try to use a PhantomJS driver for my Selenium tests but I don't succeed to recover my Firefox profile to avoid me login in the website.
This is my code :
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Stackoverflow {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
System.setProperty("phantomjs.binary.path", System.getProperty("user.dir") + "/lib/phantomjs-2.1.1-windows/bin/phantomjs.exe");
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
driver = new PhantomJSDriver(capabilities);
driver.manage().window().maximize();
baseUrl = "http://stackoverflow.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testStackoverflow() throws Exception {
driver.get(baseUrl);
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
Can you tell me how to set PhantomJS driver ?
You are unable to use a firefox profile with PhantomJS because you're trying to use a firefox profile with PhantomJS... PhantomJS is not firefox, how did you think that was going to work.
Use this code to set the path of the phantomjs driver, Use this and let me know if you facing any issue:
File file = new File("E:\\software and tools\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
OR
System.setProperty("phantomjs.binary.path", "E:\\software and tools\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
WebDriver driver = new PhantomJSDriver();
Related
On Eclipse, The error red line was showing below the ChromeDriver statement and Line 18 (WebDriver driver = new ChromeDriver). Is that code correct?
Code:
package firsttestngpackage;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class FirstTestNGFile {
public String baseUrl = "http://demo.guru99.com/test/newtours/";
String driverPath=
"C:\\Users\\manoj\\Documents\\QA\\COURSE\\ChromeDriver94\\chromedriver.exe";
//public WebDriver driver;
#Test
public void f() {
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.driver", driverPath);
WebDriver driver = new ChromeDriver();
driver.get(baseUrl);
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
driver.close();
}
}
Yes it looks fine to me.
Additionally, you'd have to import WebDriver as well.
import org.openqa.selenium.WebDriver;
If that still did not work, I would suggest to take WebDriver driver out of #Test
Sample code :
private WebDriver driver;
public String baseUrl = "http://demo.guru99.com/test/newtours/";
String driverPath = "C:\\Users\\manoj\\Documents\\QA\\COURSE\\ChromeDriver94\\chromedriver.exe";
#Test
public void f() {
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.get(baseUrl);
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
driver.close();
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
// Few important imports in selenium to look.
// WebDriver interface is important for using methods
get(),getCurrentUrl(),findElement(),close(),quit() etc
This question already exists:
How to run a Java Selenium test on an incognito Google Chrome profile
Closed 2 years ago.
I hope that you're fine. I'm new at Java Selenium automation, what I want to do is I want to run a test on a certain website but not in a clean session of ChromeDriver I would like to use a certain profile of mine I name it "test" its shortcut in the Desktop is "test.lnk", here what I did:
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
//import org.openqa.selenium.edge.EdgeDriver;
import org.testng.annotations.Test;
public class AutomationTest {
WebDriver driver;
#BeforeTest
public void setUp() {
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data/test");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
// driver = new ChromeDriver();
driver.get("https://www.rapidtables.com/tools/click-counter.html");
driver.manage().window().maximize();
}
#Test
public void testRegister() throws InterruptedException {
do {
Thread.sleep(3000);
driver.findElement(By.id("addbtn")).click();
} while(true);
}
#AfterTest
public void tearDown() {
// driver.close();
}
}
```
chrom_options.add_argument("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data")
chrom_options.add_argument("profile-directory=test)
specify only the user data folder in userdata dir argument
Pass profile directory separately
I want to extract chrome logs in java selenium web driver project. I am using following to launch chromedriver.
> dc = DesiredCapabilities.chrome();
> System.setProperty("webdriver.chrome.logFile","//src//resource//log.txt");
Another similar question has following solution.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class ChromeConsoleLogging {
private WebDriver driver;
#BeforeMethod
public void setUp() {
System.setProperty("webdriver.chrome.driver", "c:\\path\\to\\chromedriver.exe");
DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
driver = new ChromeDriver(caps);
}
#AfterMethod
public void tearDown() {
driver.quit();
}
public void analyzeLog() {
LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
for (LogEntry entry : logEntries) {
System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
//do something useful with the data
}
}
#Test
public void testMethod() {
driver.get("http://mypage.com");
//do something on page
analyzeLog();
}
}
Solution is not useful for me, as I want to save browser log for every session with the session Id itself.
I'm using flex-ui-selenium to automate my flex application, which adds 2 numbers and displays the result in an alert box.
Below is my selenium code:
package com.selenium.testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.thoughtworks.selenium.FlexUISelenium;
import com.thoughtworks.selenium.Selenium;
public class FlashSeleniumtest {
#SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\dev\\HydFramework\\Hyd\\HybridFrameWork\\jarsForAnt\\chromedriver.exe");
String BASE_URL = "C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\FlexDemo\\SeleniumTest-debug\\SeleniumTest.html";
WebDriver driver = new ChromeDriver();
//FlashObjectWebDriver driver1 = new FlashObjectWebDriver((FlashObjectWebDriver) driver,"SeleniumTest");
driver.get(BASE_URL);
driver.manage().window().maximize();
FlexUISelenium flexUITester;
flexUITester = new FlexUISelenium((Selenium) driver, "SeleniumTest");
flexUITester.type("100").at("first");
flexUITester.type("200").at("second");
flexUITester.click("Sum");
}
}
When I execute this code, I get the following exception:
"org.openqa.selenium.chrome.ChromeDriver cannot be cast to com.thoughtworks.selenium.Selenium".
Please help.
I am using Selenium 2.
But after running following code, i could not able to type in textbox.
package Actor;
import org.openqa.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.junit.*;
import com.thoughtworks.selenium.*;
//import org.junit.Before;
public class Actor {
public Selenium selenium;
public WebDriver driver;
#Before
public void setup() throws Exception{
driver = new FirefoxDriver();
driver.get("http://www.fb.com");
}
#Test
public void Test() throws Exception{
//selenium.type("id=gs_htif0", "test");
System.out.println("hi");
// driver.findElement(By.cssSelector("#gb_1 > span.gbts")).click();
selenium.waitForPageToLoad("300000000");
WebElement email=driver.findElement(By.id("email"));
email.sendKeys("nshakuntalas#gmail.com");
driver.findElement(By.id("u_0_b")).click();
}
#After
public void Close() throws Exception{
System.out.println("how are you?");
}
}
This is simple if you only use Selenium WebDriver, and forget the usage of Selenium-RC. I'd go like this.
WebDriver driver = new FirefoxDriver();
WebElement email = driver.findElement(By.id("email"));
email.sendKeys("your#email.here");
The reason for NullPointerException however is that your variable driver has never been started, you start FirefoxDriver in a variable wb thas is never being used.
Thanks Friend, i got an answer. This is only possible because of your help. you all give me a ray of hope towards resolving this problem.
Here is the code:
package facebook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Facebook {
public static void main(String args[]){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.facebook.com");
WebElement email= driver.findElement(By.id("email"));
Actions builder = new Actions(driver);
Actions seriesOfActions = builder.moveToElement(email).click().sendKeys(email, "gati.naveen#gmail.com");
seriesOfActions.perform();
WebElement pass = driver.findElement(By.id("pass"));
WebElement login =driver.findElement(By.id("u_0_b"));
Actions seriesOfAction = builder.moveToElement(pass).click().sendKeys(pass, "naveench").click(login);
seriesOfAction.perform();
driver.
}
}
You should replace WebDriver wb = new FirefoxDriver(); with driver = new FirefoxDriver(); in your #Before Annotation.
As you are accessing driver object with null or you can make wb reference variable as global variable.
Try this :
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("emal#gmail.com");
Another way to solve this using xpath
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath(//*[#id='email'])).sendKeys("your#email.here");
Hope that will help. :)
You can use JavaScript as well, in case the textfield is dithered.
WebDriver driver=new FirefoxDriver();
driver.get("http://localhost/login.do");
driver.manage().window().maximize();
RemoteWebDriver r=(RemoteWebDriver) driver;
String s1="document.getElementById('username').value='admin'";
r.executeScript(s1);