Showing posts with label firefox. Show all posts
Showing posts with label firefox. Show all posts

20.11.15

Configure QTP to work with Firefox

Steps to configure QTP to work with Firefox 10.0.3, so that QTP is able to identify objects on Firefox.

  • Install QTP and Un-install any previous instances of Firefox
  • Install Firefox 10.0.3 by opening the file "WX7-FireFoxESR-10-0-3-R1.EXE" in "Run as Admin" mode
  • Restart the system
  • Install the QTP Patches in the following sequence:
    • a. QTPWEB_00090.EXE
    • b. QTPWEB_00092.EXE
  • Info about installed Patches would be available at: C:\Program Files (x86)\HP\QuickTest Professional\HotfixReadmes
  • Restart the system
  • Open: C:\Program Files (x86)\HP\QuickTest Professional\bin\Mozilla\Common\install.rdf
  • Copy the em:ID for QTP, at the top of the file, not for the Firefox itself.
  • Create a new empty file with this ID as the file name, do not give any extension to this file. Eg: {9F17B1A2-7317-49ef-BCB7-7BB47BDE10F8}
  • Enter this line in the file and click Save: C:\Program Files (x86)\HP\QuickTest Professional\bin\Mozilla\Common
  • Paste this file at: C:\Program Files (x86)\Mozilla Firefox\extensions
  • Admin privileges are need to drop this file!
  • On the desktop shortcut for QTP, right click and select "Run as Admin", let QTP get opened completely
  • Open Firefox, and install the QTP Plug-in Add-on when prompted

After this, QTP should be able to identify the objects on Firefox.
[This post needs to be updated for UFT and latest firefox versions]     

21.6.15

Run Selenium tests on IE and Chrome browsers

Download the driver EXEs from www.seleniumhq.org, for the OS version you have. Since firefox driver is bundled in the selenium jar, we do not need to download the firefox driver separately, however, we do have to configure it, just like the other drivers.

Create a folder called 'drivers' under the 'resources' folder, in your project structure

Extract and copy the 'IEDriverServer.exe' and 'ChromeDriver.exe' files in the 'drivers' folder.




This below code demonstrates how you can choose the browser that you want to run your tests in - all you have to do is pass the name of the browser as an input to the Driver method.


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Drivers {


    private WebDriver driver;

    String ieDriverPath = "src\\resources\\drivers\\IEDriverServer.exe";
    String chromeDriverPath = "src\\resources\\drivers\\ChromeDriver.exe";

    public Drivers(String browser){

        setDriver(browser);
    }

    public WebDriver getDriver(){

        return driver;
    }

    public void setDriver(String browser){


        String downloadFolderPath = "C:\\z_All_Downloads\\Java";


        this.driver = null;



        if (browser.equalsIgnoreCase("IE")){

            System.setProperty("webdriver.ie.driver", ieDriverPath);

            InternetExplorerDriver ieDriver = new InternetExplorerDriver();
            this.driver = ieDriver;
        }

        if (browser.equalsIgnoreCase("CHROME")){


            System.setProperty("webdriver.chrome.driver", chromeDriverPath);

            ChromeDriver chromeDriver = new ChromeDriver();
            this.driver = chromeDriver;
        }

if (browser.equalsIgnoreCase("FIREFOX")) {


//creating an object for the profiles

            ProfilesIni allProfiles = new ProfilesIni();

//getting the firefox profile for webdriver, which was created manually

            FirefoxProfile profile = allProfiles.getProfile("webdriverprofile");

//Setting the firefox preferences

// Setting the preferences for download -
            profile.setPreference("browser.download.folderList", 2);
            profile.setPreference("browser.download.dir", downloadFolderPath);
            profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

//Prevent Firefox from previewing PDFs -

            profile.setPreference("pdfjs.disabled", true);

//Disabling the third party PDF viewers -

            profile.setPreference("plugin.scan.plid.all", false);
            profile.setPreference("plugin.scan.Acrobat", "90.0");
            profile.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf");

//instantiating the firefox browser with this new profile and preference

            WebDriver firefoxDriver = new FirefoxDriver(profile);

            this.driver = firefoxDriver;

        }


    }


}   //end of class

Common issues and troubleshooting -

  • Error - [The driver executable does not exist: C:\BDD\BDD\src\resources\data\drivers\ChromeDriver.exe]. This generally happens when the driver exe is not located at the referred location, and can be resolved by putting the exe at the correct location

2.12.14

Function to delete firefox cookies with VbScript / QTP

There is a QTP Util method to delete browser cookies, but that works only for IE.
Deleting the cookies for firefox programatically is a little tricky, specially if there are multiple profiles that you are using, but here is a function to do just that.

Function funcDeleteCookies()

''Ignoring any errors that may arise during file deletion

On Error Resume Next

''This function will work best when firefox is closed

Call funcTerminateProcess("firefox.exe")

''Defining the list of files that need to be deleted to clear the cache

'' arrFileToDel = Array("compatibility.ini","cookies.sqlite","cookies.sqlite-shm","cookies.sqlite-wal","extensions.ini","extensions.sqlite","formhistory.sqlite","key3.db","localstore.rdf","permissions.sqlite","places.sqlite","places.sqlite-shm","places.sqlite-wal","prefs.js","sessionstore.bak","sessionstore.js","urlclassifierkey3.txt","webappsstore.sqlite")

arrFileToDel = Array("cookies.sqlite","cookies.sqlite-shm","cookies.sqlite-wal","formhistory.sqlite","places.sqlite","places.sqlite-shm","places.sqlite-wal","sessionstore.bak","sessionstore.js","localstore.rdf","formhistory.sqlite","key3.db")


''Getting the current logged in user, because the path is user specific

sUserName = Environment("UserName")

''Location of the firefox profile folder

sProfileFolderFox = "C:\Users\" & sUserName & "\AppData\Roaming\Mozilla\Firefox\Profiles"

Set oFolderFSO = CreateObject("Scripting.FileSystemObject")


If Not oFolderFSO.FolderExists(sProfileFolderFox) Then


Call funcLogger(micWarning, "The firefox cache folder could not be located", "Cache not cleared. " & sProfileFolderFox)


Else


''Getting all the sub folders

Set oChildFolders = oFolderFSO.GetFolder(sProfileFolderFox).SubFolders

''Looping for each of the sub folders

For Each oFolder In oChildFolders

sCurrFolderName = oFolder.Name


''Generally its enough to clear the cache from the default folder, which is what we are checking below

If InStr(1,LCase(sCurrFolderName),".default",1) Then

''Deleting the files

For i = 0 To UBound(arrFileToDel) - 1
oFolderFSO.DeleteFile(sProfileFolderFox & "\" & sCurrFolderName & "\" & arrFileToDel(i))
Next

End If


Next


End If


''Enabling errors again

On Error GoTo 0

End Function

16.9.14

Automatically Download Files with Selenium

Selenium itself cannot interact with system-level dialogs opened by JS in the browser. In order to download PDFs as part of the browser automation process, it requires the help from either additional frameworks or an approach that handles the downloads automatically, and prevent Firefox from popping up the "Save file" dialog.

One way is to use the Firefox preferences to download the files directly without going through the dialog box, and here is the code that will do just that.

This approach is better as we don't have to use custom/third party software to deal with the dialog boxes separately, which can become tedious and difficult to integrate with the already existing complex setup we usually have for selenium.


public WebDriver getFireFoxProfileDriverForDownload(){

        String downloadFolderPath = "C:\\All_Downloads\\Java";

        //creating an object for the profiles
        ProfilesIni allProfiles = new ProfilesIni();

        //getting the firefox profile for webdriver, which was created manually
        FirefoxProfile profile = allProfiles.getProfile("webdriverprofile");

        //Setting the preferences for download -
        profile.setPreference("browser.download.folderList",2);
        profile.setPreference("browser.download.dir", downloadFolderPath);
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");

        //Prevent Firefox from previewing PDFs -
        profile.setPreference("pdfjs.disabled",true);

        //Disabling the third party PDF viewers -
        profile.setPreference("plugin.scan.plid.all",false);
        profile.setPreference("plugin.scan.Acrobat","90.0");
        profile.setPreference("plugin.disable_full_page_plugin_for_types","application/pdf");

        //instantiating the firefox browser with this new profile
        WebDriver firefoxDriver = new FirefoxDriver(profile);

        //This driver instance can now be used to open firefox with the above preferences
        return firefoxDriver;

    }


Description of the code above -
·        Setting the preferences for download -
The preferences can be set using the setPreference method of the FirefoxProfile class. This method needs the value of the preferences in key-value pairs, where the key is the name of the preference and the value is the option that you want to set it to. The name and acceptable/allowable values for the preferences can be found by browsing the 'config' properties of the firefox, via the 'about:config' page. The key would be the 'Preference Name' and the value would be the 'Value', as seen from the 'config' window of the firefox.
Firefox’s download manager preferences can be set programmatically while instantiating FirefoxDriver. The following properties can be used for this.
  • "browser.download.folderList" - This controls the default folder to download a file to. 0 indicates the Desktop, 1 indicates the systems default downloads location and 2 indicates a custom folder.
  • "browser.download.dir" - This holds the custom destination folder for downloading. It is activated if browser.download.folderList has been set to 2.
  • "browser.helperApps.neverAsk.saveToDisk" - This stores a comma-separated list of MIME types to save to disk without asking what to use to open the file.

The MIME type defined here is "application/pdf", which is a type that most PDF files use. However, if the target PDF file has a non-standard MIME type, then “Save file” dialog might still show up. In order to fix this issue, the actual MIME type has to be added into browser.helperApps.neverAsk.saveToDisk property, which can be checked out using either of the following approaches:
  • Upload file to online tools like - http://mime.ritey.com/
  • Download file and monitor MIME type in Chrome’s developer tool or web debugging proxy like Fiddler, CharlesProxy, etc.
·        Prevent Firefox from previewing PDFs -
With the release of Firefox 19.0, PDF.js has been integrated into Firefox to provide built-in ability of displaying PDF files inside browser. It tries to parse and render PDFs into HTML5, which can be automated using Selenium WebDriver in theory. However, to download PDFs instead of preview in Firefox, another about:config entry needs to be changed to disable PDF.js.

·        Disabling the third party PDF viewers -
Except for Firefox’s built-in PDF viewer, there might be other third party plugins preventing Firefox from downloading PDFs automatically. If a machine has Adobe Reader installed, then default PDF viewing setting in Firefox might have been set to Adobe Acrobat without notice.
To avoid previewing PDFs with those plugins, two more about:config entries need to be configured when starting WebDriver instance.
  • "plugin.scan.plid.all" - This needs to be set to false, so that Firefox won’t scan and load plugins.
  • "plugin.scan.Acrobat" - This is a key that holds the minimum allowed version number that Adobe Acrobat is allowed to launch. Setting it to a number larger than currently installed Adobe Acrobat version should do the trick.
  • "plugin.disable_full_page_plugin_for_types" = "application/pdf" - Without this, the pdf gets opened in the browser itself, instead of getting downloaded.


17.7.14

Using different Firefox Profiles with Selenium WebDriver

By default, there is just one firefox profile [called 'default'] for a given user which is used everytime one opens firefox, but using this default profile may not be the best option when running webdriver scripts.
Hence, it is better to create a new profile specifically to be used just by the webdriver.
Although, there are more powerful and effective ways to manage firefox profiles and preferences with WebDriver, but the one below is simple and effective too, and is enough for most cases. The only hitch with this method is that you will have to manually create the same firefox profile on every computer that you want to run your tests

Steps to create a new firefox profile -

•    Close all open sessions of the firefox browser
•    Type the following command on the Run window - "firefox -ProfileManager"
•    Here you will see just one profile initially, the 'default' one
•    Create a new profile and name it as 'webdriver'
•    Launch this profile and perform any configurations like disabling/enabling certain add-ons, maximize, etc
•    Close the firefox browser.
•    Now, whenever this profile will be opened, it will open with the configurations that you made.



Steps to use this new profile with webdriver -

    ProfilesIni profilesIni = new ProfilesIni();

    FirefoxProfile profile = profilesIni.getProfile("webdriverprofile");

    WebDriver foxdriver = new FirefoxDriver(profile);

We might also encounter issues with security certificates on firefox, so to overcome those, the following can be used

If the certificate is valid but self-signed
    profile.setAssumeUntrustedCertificateIssuer(true); //this is the default setting

If the certificate is invalid
    profile.setAssumeUntrustedCertificateIssuer(false);


Using this code above, the webdriver will open firefox with the profile specified, and not the default one.