How can I start the selenium Microsoft Edge? - java

I'm learning about Selenium webdriver test using Java to test my application.
I'm already implemented my tests to Google Chrome and Firefox, but the msedgedriver.exe is not opening the edge browser.
I'd like some help!
public WebDriver getDriver() {
return driver;
}
#BeforeAll
/*--- INICIALIZAÇÃO DO OGFIN*/
public void abrirOgfin() throws Exception {
String link;
int x=2;
switch(x) {
case 1:
System.setProperty("webdriver.gecko.driver", "C:\\workspace\\conf\\firefox\\geckodriver.exe");
driver = new FirefoxDriver();
//link = "http://sistema.ogfin.com.br/ogfin/Login"; //Servidor
link = "http://192.168.1.46:8888/ogfin/Login"; //Local
break;
case 2:
System.setProperty("webdriver.edge.driver", "C:\\workspace\\conf\\edge\\msedgedriver.exe");
driver = new EdgeDriver();
//link = "http://sistema.ogfin.com.br/ogfin/Login"; //Servidor
link = "http://192.168.1.46:8888/ogfin/Login"; //Local
break;
default:
System.setProperty("webdriver.chrome.driver", "C:\\workspace\\conf\\chromedriver.exe");
driver = new ChromeDriver();
link = "http://sistema.ogfin.com.br/ogfin/Login"; //Servidor
//link = "http://192.168.1.46:8888/ogfin/Login"; //Local
}
driver.manage().window().maximize();
driver.get(link);
}
When I run the code, the console show:
Starting MSEdgeDriver 75.0.139.20 (02d0ed4079b152f381df65e7da8a795530021fb1) on port 9579
Only local connections are allowed.
Please protect ports used by the WebDriver and related test frameworks to prevent access by malicious code.

Related

java.lang.NullPointerException when using webdriver manager selenium java

While using selenium with java, WebdriverManager is not running and the below code is giving null pointer exception. I have returned the driver at end of class.
I have one ask whether should I keep the Webdriver driver as static or not.
import io.github.bonigarcia.wdm.WebDriverManager;
public class Browserselector {
public WebDriver driver;
public static Properties prop;
public WebDriver initializeDriver() throws IOException {
{
String browserName = "firefox";
System.out.println(browserName);
if (browserName.contains("Chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if (browserName.contains("IE")) {
WebDriverManager.iedriver().setup();
driver = new InternetExplorerDriver();
} else if (browserName.contains("FireFox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
} else if (browserName.contains("EDGE")) {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
}
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("google.com");
return driver;
}
}
Thanks for your help in advance.
you are trying to start "firefox" - but the if condition checks for "Firefox", if you want to use it like that change the following condition
browserName.contains("FireFox")
into
browserName.equalsIgnoreCase("FireFox")
I recommend you to change the nested if with a "switch" it's more readable and easy to follow/understand
Also, don't use a URL without specifying the protocol
driver.get("https://www.google.com");

URL is not opening in chrome in selenium

This is my code and requirement is that, need to open URL in same local chrome path. Is it possible??
public class Test{
public static void main(String args[]){
System.setProperty("webdriver.chrome.driver","C://Program Files (x86)//Google//Chrome//Application//chrome.exe");
WebDriver d= new ChromeDriver(); ;
String baseurl = "http://www.facebook.com/";
d.get(baseurl);
}
}
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\chromedriver83\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseurl = "http://www.facebook.com/";
driver.get(baseurl);
Check the path of chromedriver.exe carefully from your machine.
For Windows users:
System.setProperty("webdriver.chrome.driver","C:\\Users\\user\\Downloads\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");
For MAC users:
System.setProperty("webdriver.chrome.driver","//Users//apple//Downloads//chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");

Dynamically choose driver type in Appium in order to write 'hybrid' tests

I use Appium with Java to automate tests for mobile applications. It is clear that when I want to write tests for Android I use AndroidDriver<MobileElement> driver = [..] and for iOS I need to use IOSDriver<MobileElement> driver = [..] though with this approach I'd need to write same tests twice for iOS and Android. Is there a way that I could choose type of Appium Driver dynamically based on i.e. some kind of variable to choose between AndroidDriver and iOSDriver? I tried:
if(platform == "Android"){
//returns AndroidDriver
AppiumDriver<MobileElement> driver = COMMON.startAndroid(name, id, platform, version);
} else {
//returns IOSDriver
AppiumDriver<MobileElement> driver = COMMON.startIOS(name, id, platform, version);
}
but below in Test Eclipse points out that with this approach driver is not defined
Both of those drivers extend WebDriver interface (via inheritance). You can define driver from this type. It is also OOP encapsulation concept
WebDriver driver;
if(platform.equals("Android")){
driver = COMMON.startAndroid(name, id, platform, version);
} else {
driver = COMMON.startIOS(name, id, platform, version);
}
public class AppiumController {
public static OS executionOS = OS.ANDROID;
public AppiumDriver<MobileElement> driver;
DesiredCapabilities capabilities = new DesiredCapabilities();
public enum OS {
ANDROID,
IOS,
MOBILEBROWER_IOS,
MOBILEBROWER_ANDROID
}
public void start() {
if (driver != null) {
return;
}
switch(executionOS){
case ANDROID:
// set android caps
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
case IOS:
// set ios caps
driver = new IOSDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
case MOBILEBROWER_IOS:
// set ios browser caps
driver = new IOSDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
case MOBILEBROWER_ANDROID:
// set android browser caps
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
}
public void stop() {
if (driver != null) {
driver.quit();
driver = null;
}
}
}

Selenium Webdriver 3- URL is not getting entered into the Firefox Browser

Windows 10 - 32 bit
Selenium Version:
3.0.0 beta 3
Browser:
Firefox 48.02
Eclipse Luna 32 bit
package newpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MyClass {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty "webdriver.firefox.marionette","D:\\Selenium\\geckodriver.exe");
//System.setProperty("webdriver.gecko.driver","D:\\Selenium\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://newtours.demoaut.com";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Firefox and direct it to the Base URL
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page witht the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Firefox
driver.close();
// exit the program explicitly
System.exit(0);
}
}
Error:
org.openqa.selenium.firefox.NotConnectedException: Unable to connect
to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
les":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"1.5","maxVersion":"9.9"}],"targetPlatforms":[],"multiprocessCompatible":false,"signedState":0,"seen":true}
This kind of issues are coming in selenium 3.0 beta version.
If you are using using Selenium Standalone jar then you have to pass marionette as capabilities and initialize FirefoxDriver as follows:
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
Tried with geckodriver v 0.10.0.
String driverPath = "<path to gecko driver executable>";
public WebDriver driver;
public void launchBrowser() {
System.out.println("launching firefox browser");
System.setProperty("webdriver.gecko.driver", driverPath+"geckodriver.exe");
driver = new FirefoxDriver();
}

How to get focus on firefox driver in conjunction with TestNG implementing ITestListener

I'd appreciate and thank for any advice here. I am trying to user the driver object from POM framework by use of TestNG and implementing ITestListerner interface.
This is a TestNG class implementing ItestListener
public class TestNGListener implements ITestListener {
#Override
public void onFinish(ITestContext result) {
WebDriver driver = BrowserFactory.LanuchBrowser("firefox", "http://10.207.182.108:81/opencart/");
Util.takescreenshot(driver, result.getName());
}
This class is used to return a WebDriver Object after launch one of the browser from switch case. I am using same driver as apart of TestNG by implementing Itestlistener & overriding failure public void onFinish(ITestContext result) in the above class & unfortunately, it doesn't return a webdriver object & take a screen shot but launches a new browser instead.
public class BrowserFactory {
static WebDriver driver;
public static WebDriver LanuchBrowser(String Brwsr, String URL){
System.out.println(Brwsr.toLowerCase());
switch (Brwsr.toLowerCase()){
case "firefox":
driver=new FirefoxDriver();
break;
case "chrome":
driver= new ChromeDriver();
System.setProperty("WebDriver.chrome.driver", "chromedriver.exe");
break;
case "internet explorer":
System.out.println("IE");
driver= new InternetExplorerDriver();
System.setProperty("WebDriver.IE.driver","IEDriverServer.exe");
break;
default:
System.out.println("Please select one of the Browsers listed : Chrome,Firefox or InternetExplorer");
break;
}
driver.manage().window().maximize();
driver.get(URL);
return driver;
}
}
Here is my method to capture screenshot
public class Util {
final static Logger logger = Logger.getLogger(Util.class);
public static void validatePgeNavgtn(WebDriver driver, String PgeTitle){
PropertyConfigurator.configure("log4j.properties");
String pgtitle=driver.getTitle();
if (pgtitle.equalsIgnoreCase(PgeTitle)){
logger.info("title matched");
}
}
public static void takescreenshot(WebDriver driver, String screen){
TakesScreenshot ts = (TakesScreenshot)driver;
File src= ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(src, new File("./Screenshot/"+screen+".png"));
} catch (IOException e) {
logger.warn(e.getMessage());
}
}
Could you please suggest an approach that below method shouldn't launch a new browser session but the focus should be retained on first browser instance & also capture a screenshot
You need create BrowserFactory class as singleton which will create always give single instance of BrowserFactory as below :-
public class BrowserFactory {
private WebDriver driver;
private static BrowserFactory browserFactoryInstance = null;
private BrowserFactory(String Brwsr, String URL) {
System.out.println(Brwsr.toLowerCase());
switch (Brwsr.toLowerCase()) {
case "firefox":
this.driver = new FirefoxDriver();
break;
case "chrome":
System.setProperty("WebDriver.chrome.driver", "chromedriver.exe");
this.driver = new ChromeDriver();
break;
case "internet explorer":
System.out.println("IE");
System.setProperty("WebDriver.IE.driver", "IEDriverServer.exe");
this.driver = new InternetExplorerDriver();
break;
default:
System.out
.println("Please select one of the Browsers listed : Chrome,Firefox or InternetExplorer");
break;
}
this.driver.manage().window().maximize();
this.driver.get(URL);
}
public static BrowserFactory getInstance(String Brwsr, String URL) {
if(browserFactoryInstance == null) {
browserFactoryInstance = new BrowserFactory(Brwsr, URL);
}
return browserFactoryInstance;
}
public WebDriver getDriver() {
return this.driver;
}
}
Now you can get WebDriver instance as below :-
//It will return always single instance per test run
WebDriver driver = BrowserFactory.getInstance("firefox", "http://10.207.182.108:81/opencart/").getDriver();
Util.takescreenshot(driver, result.getName());

Categories

Resources