开发者

Selenium 2 WebDriver to use Custom Profile

I'm trying to automate the interaction with a website that generates documents with MIME type application/vnd.wap.xhtml+xml. I am using Selenium 2, the WebDriver and the FirefoxProfile.

Because Firefox does not handle the above mentioned MIME type, I need to run Firefox with the XHTML Mobile Profile extension (https://addons.mozilla.org/en-US/firefox/addon/1345/).

After creating a FireFox profile -I named it 'selenium'- and installing the Mobile Profile extension, I tried to use the code snippets in the 'Tips and Tricks' section of the 'Selenium 2.0 and WebDriver' document (http://seleniumhq.org/docs/09_webdriver.html#htmlunit-driver).

Approach #1 looks like this:

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("selenium");
profile.setPreference("general.useragent.override", "User Agent string to force application/vnd.wap.xhtml+xml content..");
FirefoxDriver driver = new FirefoxDriver(profile);
driver.get("http://www.mobilesite.com/");
WebElement element = driver.findElement(By.tagName("body"));

Approach #2 looks like this:

File profileDir = new File("/path/to/custom/profile/with/extension/ffprofile");
FirefoxProfile profile = new FirefoxProfile(profileDir);
profile.setPrefere开发者_如何学编程nce("general.useragent.override", "same user agent string as above");
FirefoxDriver driver = new FirefoxDriver(profile);
driver.get("http://www.mobilesite.com/");

No matter what code snippet I use, the browser instance that starts up is always unable to handle the generated content; the browser prompts me for an action to take on the content of the unrecognized MIME type as if the extension was not correctly configured.

Any ideas on what I could be doing wrong?

Thanks in advance,

Edit: Link to Selenium users group post.


Try starting with a blank profile and adding extensions/configurations at runtime:

public WebDriver getDriver() {
    FirefoxProfile profile = new FirefoxProfile();

    // add any custom firefox configurations...
    profile.setPreference("general.useragent.override", "some UA string");
    profile.setPreference("javascript.options.showInConsole", true);
    profile.setPreference("dom.max_script_run_time", 0);

    // might have to uninstall, search for *.xpi, then reinstall, then search 
    // again and compare to find the location on your system
    // ...you should probably copy this into your selenium resources directory!
    File modifyHeadersXpi = new File("/home/joecoder/.mozilla/firefox/dll8peh9.default/extensions/{b749fc7c-e949-447f-926c-3f4eed6accfe}.xpi");
    if (modifyHeadersXpi.exists()) {
        try {
            profile.addExtension(modifyHeadersXpi);
            profile.setPreference("modifyheaders.config.active", true);
            profile.setPreference("modifyheaders.config.openNewTab", true);
            profile.setPreference("modifyheaders.config.migrated", true);
            profile.setPreference("modifyheaders.autocomplete.name.defaults", 
                    "[\"Accept\",\"Cache-Control\",\"Cookie\",\"Content-Length\",\"Content-Type\",\"Date\",\"Host\",\"Pragma\",\"Referer\",\"User-Agent\",\"Via\",\"X-Requested-With\",\"X-Forwarded-For\",\"X-Do-Not-Track\"]");
        }
        catch (IOException e) { /* uh oh */ }
    }
    return new FirefoxDriver(profile);
}


Hope this will help you out:

public class Wap {

public static void main (String[] args) throws IOException{ 

FirefoxProfile profile = new FirefoxProfile();
String baseURL;
profile.addExtension(new File("C:\\Users\\Pandu\\Desktop\\WAP\\modify_headers-0.7.1.1-fx.xpi"));

profile.setPreference("modifyheaders.config.active", true);
profile.setPreference("modifyheaders.config.alwaysOn", true);
profile.setPreference("modifyheaders.headers.count", 2);
profile.setPreference("modifyheaders.headers.action0", "Add");
profile.setPreference("modifyheaders.headers.name0", "X-Nokia-msisdn");
profile.setPreference("modifyheaders.headers.value0", "123456789");
profile.setPreference("modifyheaders.headers.enabled0", true);
profile.setPreference("modifyheaders.headers.action1", "Add");
profile.setPreference("modifyheaders.headers.name1", "x-sec-pass");
profile.setPreference("modifyheaders.headers.value1", "sdp123");
profile.setPreference("modifyheaders.headers.enabled1", true);


    Logger Log = Logger.getLogger(WebDriver.class.getName());

    WebDriver driver = new FirefoxDriver(profile);
    try{
driver.get("http://www.google.com");

        driver.findElement(By.linkText("Telugu")).click();


You have to make sure you add the browser plugin as a DeploymentItem in your testsettings file. Some examples (in this one we have added Firebug):

  <Deployment>
    <DeploymentItem filename="Selenium\firebug@software.joehewitt.com.xpi" />
    <DeploymentItem filename="packages\Castle.Core.3.1.0\lib\net40-client\Castle.Core.dll" />
    <DeploymentItem filename="Selenium\IEDriverServer.exe" />
    <DeploymentItem filename="Selenium\chromedriver.exe" />
    <DeploymentItem filename="Selenium\skipcerterror@foudil.fr.xpi" />
  </Deployment>

You will then need to create a profile that looks something like this:

string firebugPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "", "firebug@software.joehewitt.com.xpi");

FirefoxProfile firebugProfile = new FirefoxProfile() {AcceptUntrustedCertificates = true};
firebugProfile.AddExtension(firebugPath);
firebugProfile.SetPreference("extensions.firebug.currentVersion", "1.10.3");
firebugProfile.SetPreference("extensions.sce.bypass_domain_mismatch", true);
firebugProfile.SetPreference("webdriver_assume_untrusted_issuer", false);

Driver = new FirefoxDriver(firebugProfile);
Driver.Manage().Window.Maximize();

If you add the extension using AddExtension, it should be available within you selenium driver. I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜