2

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.

Eugene S
  • 6,709
  • 8
  • 57
  • 91
drs
  • 35
  • 6
  • What does `file.getAbsolutePath()` return? It seems redundant to use that when the path is already an absolute path. – JeffC Jul 06 '16 at 22:23
  • Why are you declaring file twice? Once as a `final` in the class and once in `setup()`? – JeffC Jul 06 '16 at 22:25
  • `file` is declared twice but commented out in either instance prior to compilation. The double-declaration was used to test whether placing it in `setup()`. If I'm not mistaken, `file.getAbsolutePath()` is just being used to pass the path from `file` into `System.setProperty()`. This is recycled code, though, and you're probably right on the redundancy. Maybe I should cut out the middleman and replace `System.setProperty()` with the absolute path directly? That should allow me to skip declaration all together. – drs Jul 07 '16 at 05:07

1 Answers1

1

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.

Amit
  • 136
  • 4
  • This actually fits the answer format better than the comment format. – The SE I loved is dead Jul 06 '16 at 23:44
  • The main method is `setup()` in this instance, or so I think.. And yes, I assumed that by declaring it in the class as Public Static Final that it would be visible in `setup()`. Is that not the case? Should I declare a specific `Main(String[] args)` instead? – drs Jul 07 '16 at 05:13
  • Alright here is what i think issue might be . You are trying to initiate browser even before setting the path for webdriver.ie.driver public static final WebDriver driver =newInternetExplorerDriver(desCaps); You might want to set the property webdriver.ie.driver before initiating the driver. – Amit Jul 07 '16 at 17:36
  • I did what you suggested, and that resolved the issue - sort of. My code compiled, brought up a Java Application window, and then sort of stalled. What this made me realize is that Selenium probably doesn't like to try and activate a webdriver instance from inside a JavaFX stage, and as a result I did away with the JavaFX code and the extension of the Application class all together. I declared a specific public static final Main() method, and called the `setIEDesCaps()` method on a new instance of the `Login()` class - and now it works! Thanks for the help. – drs Jul 07 '16 at 19:58