Saturday, December 17, 2011

Clear Browser Cache

We can press Control + F5 using webdriver to clear the browser cache.

            Actions actionObject = new Actions(driver);
            actionObject.keyDown(Keys.CONTROL).sendKeys(Keys.F5).keyUp(Keys.CONTROL).perform();

Disabling caching in ChromeDriver

For disabling caching in ChromeDriver we can use the chrome switches with  DesiredCapabilities

DesiredCapabilities dc = DesiredCapabilities.chrome();
                ArrayList<String> switches = new ArrayList<String>();
                switches.add("--proxy-server="+InetAddress.getLocalHost().getHostAddress()+ ":" + Integer.parseInt(port));
                switches.add("--proxy-bypass-list=local.addthis.com");
                switches.add("--disable-application-cache");
                dc.setCapability("chrome.switches", switches);

More details about chrome switches

Maven ant run plugin

Simulate all the tasks of ant script and also used to call external ant targets from a maven pom.


<groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <configuration>
      <tasks>
        <echo message="hello ant, from Maven!" />
        <echo>Maybe this will work?</echo>

        <ant antfile="${basedir}/build.xml">
          <target name="test" />
        </ant>

       <exec dir="${basedir}" executable="cmd"
         failonerror="true">
         <arg line="/K start" />
         <arg line="one.bat" />
       </exec>

      </tasks>
    </configuration>
   </plugin>
Sample Usage : mvn antrun:run

Tuesday, November 1, 2011

Disabling caching in FirefoxDriver

 Firefox profile with no caching enabled.

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.cache.disk.enable", false);
profile.setPreference("browser.cache.memory.enable", false);
profile.setPreference("browser.cache.offline.enable", false);
profile.setPreference("network.http.use-cache", false);
WebDriver driver = new FirefoxDriver(profile);

Similarly profile with alert, prompt etc disabled

  profile.setPreference("capability.policy.policynames", "strict") ;
  profile.setPreference("capability.policy.strict.Window.alert", "noAccess") ;
  profile.setPreference("capability.policy.strict.Window.confirm", "noAccess") ;
  profile.setPreference("capability.policy.strict.Window.prompt", "noAccess") ;

Saturday, October 29, 2011

CSS selectors

For selecting a particular column in a table.

XPATH : //table[@class='stb']//tr[2]/td[2]

CSS : css=table.stb tr:nth-child(2)>td:nth-child(1)

XPATH : //input[@name="myName"]

CSS : css=input[name=myName]

 XPATH : //a[@title="Sign In"]

CSS : css=a[title='Sign In']

css=a.addthis_counter>a:nth-child(2)

a class="addthis_counter addthis_bubble_style"  //With space btw css
driver.findElement(By.cssSelector("a[class~='addthis_bubble_style']"));

More information here

Using properties file

There are a number of ways to perform data driven testing. Here i will mention how to use a properties file to get data and use it for testing purpose.   


        InputStream is = this.getClass().getResourceAsStream("/conf/datasuite.properties"); 
        Properties prop = new Properties(); 
        prop.load(is);
        String directory = prop.getProperty("Directory"); 
        String numberOfFiles = prop.getProperty("NumberOfFiles"); 
        String  fileExtension = prop.getProperty("Extension"); 
        is.close();
       
        System.out.println(directory+" "+numberOfFiles+" "+fileExtension);

Contents of datasuite.properties file.

       Directory = C:/prodFiles/
       NumberOfFiles = 25
       Extension = javaworlddfg


One thing to note is that the 'conf' folder is referenced with respect to the class files created.

Friday, October 28, 2011

Check mails using Java

 Instead of automating logging into gmail and checking each mail use the below code to get the contents of required mail 

Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");
        try {
            Session session = Session.getDefaultInstance(props, null);
            Store store = session.getStore("imaps");
            store.connect("imap.gmail.com", "xxxxx@gmail.com", "xxxxxx");
            System.out.println("Store : "+store);

            Folder inbox = store.getFolder("Inbox");
            inbox.open(Folder.READ_ONLY);
            System.out.println("Messge count "+inbox.getUnreadMessageCount());

            Message messages[] = inbox.search(new SubjectTerm("Flags.Flag (JavaMail API documentation)"), inbox.getMessages());
            Date date = new Date();
            System.out.println("Date "+date);

            for (Message message : messages) {
                System.out.println("Seen Flag "+message.isSet(Flags.Flag.SEEN));
                System.out.println("Messge Send Date "+message.getSentDate());

                if(!message.isSet(Flags.Flag.SEEN)){
                    if(date.compareTo(message.getSentDate())<=0){
                        String line;
                        StringBuffer buffer = new StringBuffer();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(message.getInputStream()));
                        while ((line = reader.readLine()) != null) {
                            buffer.append(line);
                        }
                        System.out.println(buffer);
                    }
                }
            }
        } catch (MessagingException e) {
            e.printStackTrace();
            System.exit(2);
        }