Search code examples
javainternet-explorerseleniumbrowser-automationselenium-iedriver

File path declaration not visible to Method call in Main method


I'm working on developing an application that will be used to automate form-filling operations in Java with Selenium. I've currently got both set up to be portable on a thumbdrive. My code is below:

package AutoFill;

import java.io.File;
import java.util.concurrent.*;
import javafx.application.Application.*;
import javafx.application.*;
import javafx.stage.Stage;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.ie.*;
import org.openqa.selenium.ie.InternetExplorerDriver.*;

public class Login extends Application {

    public static final File file = new File("E:/IEDriverServer_Win32_2.53.1/IEDriverServer.exe");      //path to IEDriver on USB stick
    public static final DesiredCapabilities desCaps = DesiredCapabilities.internetExplorer();           //new desired capabilities object to set IEDriver run params
    public static final WebDriver driver = new InternetExplorerDriver(desCaps);                         //new IEDriver instance
    public static final String url = new String("url_here");                                    //starting url  


@Override
public void start(Stage primaryStage) { 
    primaryStage.show();

}

public void setup() {

    File file = new File("E:/IEDriverServer_Win32_2.53.1/IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());               //force IEDriver path
    setIEDesCaps(desCaps);                                                           //run cap setter method
    driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL,"0"));   //set screen zoom to 100% to resolve webdriver errors
    driver.get(url);                                                                 //navigate to url



}


public void setIEDesCaps(DesiredCapabilities desCaps) {                              //setter method to establish IE webdriver run params                

    desCaps.setPlatform(org.openqa.selenium.Platform.WINDOWS);
    desCaps.setCapability("EnableNativeEvents", false);
    desCaps.setCapability("ignoreZoomSetting", true);
    desCaps.setJavascriptEnabled(true);

}

}

When running this code (with a real url, of course), Eclipse generates the following error:

 java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.ie.driver system property;

I've actually attempted to manually set the driver path in two different instances - at the very beginning as a static variable (which seemed most sensible), as well as within the main setup() method. Individually (when commenting out one or the other), neither placement of the driver path was visible to the main method. This driver path was visible and worked in a prior edition of this code, before I turned the Login class into an extension of Application.

How can I position the file path in the current code so that it is visible to the main method? I feel like I'm missing something here.


Solution

  • Declaring variable as Public Static Final in class ,you should be able to access it in main method(classname.variablename). And that would be ideal place if you are not using Property file.

    If you can share Main() method i can further look into.

    Note: I do not have privilege to comment currently hence had to post. It might be not a complete answer though.