Thursday, May 28, 2015

How to refresh or reload the web pages in webdriver

Using refresh() method:

In Java,

driver.navigate().refresh();
 
Using sendkeys() method:

driver.findElement(By.id(locator)).sendKeys(F5 key);

In Java,
driver.findElement(By.id("gbqfq")).sendKeys(Keys.F5);


Using navigate().to() method:
In Java,
driver.navigate().to(driver.getCurrentUrl()); 


Using get() method:
If we know the URL then we can load the same URL again to reload the page. In Java,
driver.get("https://www.google.com.bd/");

We can also use getCurrentUrl() function to know the current URL of the page. The command would be,
driver.get(driver.getCurrentUrl());


Using sendkeys() method with ASCII code:
We can use ASCII code as argument on sendkeys() method that is equivalent to F5 key command of the keyboard.
driver.findElement(By.id("gbqfq")).sendKeys("\uE035");

Here \uE035 is the ASCII code of F5 key.

Using executeScript() method:
Using this command we can execute any JavaScript for our need. If we execute location.reload() JavaScript function then current page will be reloaded which meets our purpose. 

The command is,
driver.executeScript("location.reload()");

Thursday, May 21, 2015

Select check boxes in drop down using webdriver

Suppose if you have check boxes like below,

Before clicking drop down

After clicking drop down
How to select this check boxes,
First, click on drop down object
driver.findElement(By.xpath("//span[@id='ddcl-selInd']/span/span")).click();
for(int i=0;i<driver.findElements(By.xpath("//div[@id='ddcl-selInd-ddw']/div/div/input")).size();i++)
            {

                if(driver.findElements(By.xpath("//div[@id='ddcl-selInd-ddw']/div/div["+i+"]/input")).size()>0)
                {

                    if(driver.findElement(By.xpath("//div[@id='ddcl-selInd-ddw']/div/div["+i+"]/label")).getText().equals("IT & Telecommunications"))
                    {
                      driver.findElement(By.xpath("//div[@id='ddcl-selInd-ddw']/div/div["+i+"]/input")).click();
                        break;
                    }
                }
            }




Run the webdriver scripts in chrome browser


Download the "chromedriver.exe"   and store it in your local path.

 String chromePath="C:\\chromedriver"
 System.setProperty("webdriver.chrome.driver", chromePath);
WebDriver driver=new ChromeDriver();
driver.get("url");

Read dynamically changing objects value with webdriver using sibling

How to read a text value from dynamically changing object with webdriver

If you you the sibling text,

String actualDiscount=driver.findElement(By.xpath("//span[contains(text(), 'Sibling Text')]//following-sibling::span[1]")).getText();

From gthe above table, if you first name.
We can retrieve the Address like this

StringAddress =driver.findElement(By.xpath("//span[contains(text(), 'Bat')]//following-sibling::span[2]")).getText();


Tuesday, May 19, 2015

Opening local HTML files using webdriver

            System.setProperty("webdriver.chrome.driver", chromePath);
            WebDriver driver=new ChromeDriver();
            driver.manage().timeouts().pageLoadTimeout(100000, TimeUnit.MILLISECONDS);
            String path="C:\\input.html"
            driver.get(path);

How to move file in remote unix machine

Session session=null;
        JSch jsch = new JSch();
   
        session = jsch.getSession("username", "HostName", 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword("Password");
        session.connect();
        Channel channel = session.openChannel("sftp");
        Channel channel1=null;
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;
        ChannelSftp sftpChannel1=null;
     
        sftpChannel.cd("Destination path");

        FileInputStream fis=null;       
        fis = new FileInputStream("SourcePath"+file);
        sftpChannel.put(fis,"Destination path"+file);       

        channel.disconnect();
        fis.close();
        sftpChannel.exit();
        session.disconnect();