I'm trying to set up a type of boiler-plate code for WebDriver that I can give to anyone in my QA team to help them test. My problem is that I can't seem to get Internet Explorer working. It's throwing errors and I have no idea how to fix them or if its some sort of naming issue.
The driver files are all in my C:\ Drive.
chromedriver.exe, geckodriver.exe, IEDriverServer.exe
Errors in the code below are //commented
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.firefox.FirefoxDriver;
import org.openqa.selenium.ie.IEDriverService; //The import org.openqa.selenium.ie.IEDriverService cannot be resolved
public class Loginmethod {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\\\geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "C:\\\\chromedriver.exe");
System.setProperty("webdriver.ie.driver", "C:\\\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(); //InternetExplorerDriver cannot be resolved to a type
driver.get("http://www.google.com/");
Thread.sleep(100);
}
}
Additionally, If anyone would know a way to test for Safari using windows 10 with selenium, that would be great.
The class you are trying to import is not the class you are using.
You are importing IEDriverService but using the InternetExplorerDriver class.
change your code to import InternetExplorerDriver.
Related
I am trying to use Java Selenium's ChromeDriver to log in to my own google profile and navigate to a website. However, what I found out is that if I log in to my profile, I will not be able to navigate to any website. On the other hand, if I did not log in to my profile, I can navigate to the website.
I attach the code below for your reference, the code immediately below is able to log in to profile but unable to navigate to bing website.
package Package1;
import java.util.concurrent.TimeUnit;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
public class MyClass {
public static void main(String[] args) {
// System Property for Chrome Driver
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
String userProfile = "C:\\Users\\XXXXX\\AppData\\Local\\Google\\Chrome\\User Data";
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=" + userProfile);
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://bing.com");
}
}
The code below is able to navigate to bing website, but for my usage I need it to have a profile.
package Package1;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
public class MyClass {
public static void main(String[] args) {
// System Property for Chrome Driver
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe")
WebDriver driver = new ChromeDriver();
driver.get("https://bing.com");
}
}
I know I have imported some libraries that I did not use, that should not be the problem here. How should I edit my code to make it work?
Okay solved. If I have existing chrome browser opened then the code will not be able to log in to my profile and navigate to website. But if there are no instances of chrome browser opened prior to running the code. It will work.
Will appreciate it if anyone knows why this is the way?
As we know One of the features added in the new version of Selenium (4.0.0-alpha-2) is a very nice Interface for Chrome DevTools API in Java.DevTools API offers great capabilities for controlling the Browser and the Web Traffic
As per documentation using the latest version of selenium we can capture the network request from the session.
Before I used browsermob for getting the network request but unfortunately they didn't update it a couple of years.
I am looking for someone who used this selenium4 dev tools API for getting all the internal request.
Can anyone suggest me how can I start to get all the requests? Thanks, advance
You can find #adiohana's example in the selenium-chrome-devtools-examples repo on gitHub.
I think youed find this test example helpful:
public class ChromeDevToolsTest {
private static ChromeDriver chromeDriver;
private static DevTools chromeDevTools;
#BeforeClass
public static void initDriverAndDevTools() {
chromeDriver = new ChromeDriver();
// dev-tools handler
chromeDevTools = chromeDriver.getDevTools();
chromeDevTools.createSession();
}
#Test
public void interceptRequestAndContinue() {
//enable Network
chromeDevTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
//add listener to intercept request and continue
chromeDevTools.addListener(Network.requestIntercepted(),
requestIntercepted -> chromeDevTools.send(
Network.continueInterceptedRequest(requestIntercepted.getInterceptionId(),
Optional.empty(),
Optional.empty(),
Optional.empty(), Optional.empty(),
Optional.empty(),
Optional.empty(), Optional.empty())));
//set request interception only for css requests
RequestPattern requestPattern = new RequestPattern("*.css", ResourceType.Stylesheet, InterceptionStage.HeadersReceived);
chromeDevTools.send(Network.setRequestInterception(ImmutableList.of(requestPattern)));
chromeDriver.get("https://apache.org");
}
You need to add the following imports:
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Console;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.network.Network;
import org.openqa.selenium.devtools.network.model.BlockedReason;
import org.openqa.selenium.devtools.network.model.InterceptionStage;
import org.openqa.selenium.devtools.network.model.RequestPattern;
import org.openqa.selenium.devtools.network.model.ResourceType;
import org.openqa.selenium.devtools.security.Security;
import java.util.Optional;
I was trying to run TestNG script via Selenium WD 3.6 Version
The script just need to browse to Facebook via Chrome Browser
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class FB {
ChromeDriver driver= new ChromeDriver ();
#BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
driver.manage().window().maximize();
}
#Test
public void URL()
{
driver.get("https://www.facebook.com/");
}
}
I received the following error in console:
[RemoteTestNG] detected TestNG version 6.12.0
org.testng.TestNGException:
Cannot instantiate class Staging.FB
Please advise what i did wrong
Thanks
I'd recommend changing your driver member type to WebDriver, then, instead of initializing at the class level, do so inside your #BeforeTest method.
If we look at the TestNG lifecycle, the FB class gets instantiated before the #BeforeTest method runs. In order for that to happen, the WebDriver instance field needs to resolve. Since you appear to be using a system property to set the location of the Chrome Driver executable, this initialization cannot take place without throwing an error (since this happens in #BeforeTest). By moving the WebDriver initialization after the property declaration, initialization can run successfully.
So...
public class FB {
WebDriver driver;
#BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
// Then add your #Test method(s) here...
}
This question already has answers here:
importing classes using wildcards
(5 answers)
why doesn't Java have "deep" wildcard import? [duplicate]
(2 answers)
Closed 6 years ago.
When you export a Selenium IDE test to Java, the code imports a ton of packages. Why does it automatically put in the code:
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
This was on a very basic test that I exported from the IDE to Java. Why automatically include specific packages like selenium.firefox.FirefoxDriver and selenium.support.ui.Select if you're going to import the whole selenium.* package anyways?
Looks like you want to know about java imports. Asterisk, i.e. * will import all the TYPES, i.e classes, interfaces etc in the package which precedes it, i.e. selenium. It does NOT get all the types in its sub-packages. Refer - https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
Now you know why this code won't compile.
import org.openqa.selenium.*;
//import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.Select;
public class Temp {
public static void main(String [] args){
WebDriver driver = new FirefoxDriver();
}
}
Hi I have setUp a Java project using Maven in eclipse.
I am facing an issue whenever I am trying to run the script. Its is executed by the not opening the desired website which I am parsing from the feature file.
Please have a look to the following code and Image of my directories setup in eclipse
Here is my code for PageStepsDefs.java
package com.workshop.airport.workshop.airport;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
public class PageStepsDefs {
public String ChromeDriverPath="C:\\Users\\zain.jamshaid\\Desktop\\chromedriver.exe";
public WebDriver driver;
String localhost="www.google.com";
#Before
public void deleteAllCookies() {
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
}
#Before
public void setup(){
System.setProperty("webdriver.chrome.driver",ChromeDriverPath);
driver = new ChromeDriver();
}
#Given("^I browse to the (.+) page$")
public void open_page(String url)
{
driver.get(localhost+url);
System.out.println(localhost+url);
}
#After
public void tearDown(){
driver.quit();
}
}
Here is my code for RunCukeTest.java
package com.workshop.airport.workshop.airport;
import cucumber.api.junit.*;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#Cucumber.Options(
tags={"#mysingle"},
format={"pretty", "html:target/cucumber-html-report"},
monochrome=true,
features={"."},
strict=true)
public class RunCukeTest {
}
Here is statements in feature file
Feature: Login Functionality
#mysingle
Scenario: user successfully logins to the application
Given I browse to the / page
Any help will be awesome.
Thanks In advance.
Zain
I think I know the problem. As per your comment, the '/' from feature file is getting parsed to your step correctly. So this is not a cucumber issue. The issue I think is with your url. The url you have is incorrectly formed. URL should start with http://
I think everything will work fine if you change your localhost variable to String localhost="http://www.google.com";
Is it really executing your feature file? Try putting test.feature under src/test/resources/com/workshop/airport/workshop/airport: the JUnit running uses the unit test package as the location for finding the feature files.