Advertise

Saturday, February 2, 2013

WebDriver Alert and Prompts access

Alert alert = driver.switchTo().alert();

String alertText = alert.getText(); 

alert.accept(); // Accepting alert . Like clicking on OK button.

alert.dismiss(); // Dismiss alert . Like clicking on cancel button.

alert.sendKeys(Keys.BACK_SPACE); // Send keys. Example in my case i am sending backspace.

WebDriver Select- Select/Deselect Entries from drop down.


Select value form drop down:


WebElement option = driver.findElement(By.xpath("//path_to_drop_down"));

Select select  = new Select(options);

List<WebElement> allOptions= select.getOptions();
List<WebElement> allSelectedOptions= select.getAllSelectedOptions();

WebElement firstSelectedOption = select.getFirstSelectedOption();

select.selectByVisibleText("text to Select from drop down");

select.selectByIndex(0) //Select first index element

select.selectByIndex(1) //Select second index element

select.selectByValue("value to select")

select.deselectAll() // Clear all selected entries.

select.deselectByValue("deselect field value") // As name suggest deselect selected entry based on input.

select.deselectByIndex(0) // As name suggest deselect selected entry based on index.

select.deselectByIndex(1) // As name suggest deselect selected entry based on index.

select.deselectByVisibleText("deselect from drop down") // As name suggest deselect selected entry based on visible text.

Saturday, August 25, 2012

Screen Shot capture using selenium and storing with test method name

Taking Screen Shot using Selenium Webdriver.

Sample example to  help all in taking screen shot using selenium webdriver. It has been quit sometime i stopped posting update in my blog. 

So, Found this way to update this blog.Few more to come.






import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Listeners;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.reporters.JUnitReportReporter;


/**
 *  Sample Test Case to show case how screen shot can be captured and stored with name of
 *  Test method. This would be useful when multiple test method are present in single java file
 */

@Listeners(SeleniumRunnerExample.class)
@Test(groups={"Sample1"})
public class SeleniumRunnerExample extends JUnitReportReporter
{
    WebDriver driver = null;
    ITestNGMethod method;


    @BeforeSuite
    public void driverInstantiationTest(){              
        ProfilesIni allProfiles = new ProfilesIni();
    
      // Creating custom profile of Firefox and referring to same.

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

        //UIAutomation refer to profile created by user.This custom profile will be invoked by               
        //Webdriver for performing required activity on browser.

        profile.setAcceptUntrustedCertificates(false);

     
       // Setting whether to accept untrusted Certificate or Not.

        driver = new FirefoxDriver(profile);

       // New instance of Firefox driver with custom profile

        driver.get("https://www.example.com");
    
       // Opening https://www.example.com. Here https://www.example.com is just for example       
       // Purpose
      
    }  

  
    @Parameters({"url"})               //Parameter that is retrieved from "testng-suite.xml"
    @Test(description="Clear description of what this test method is doing should be updated here")
    public void exampleTest(String url){
    /**
     *   Sample implementation part should be written here.
     *
     * */
    }
  
    //takeScreenShotTest is having @ AfterMethod annotation. Since i want to take screen shot after      //each test case has been Run. So as to know where it terminated at last.



    @AfterMethod(description="Takes screen shot")
    public void takeScreenShotTest(ITestResult result) throws IOException{
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("D:\\\test-output\\screenshot"+result.getMethod().getMethodName()+".png"));
    }
  
    @AfterSuite
    public void closeBrowser(){
        driver.quit();
    }

}

Saturday, March 17, 2012

Which part of Selenium is appropriate for me?

  • Selenium IDE


Selenium IDE is an integrated development environment for Selenium scripts. It is implemented as a Firefox extension, and allows you to record, edit, and debug tests. Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly record and play back tests in the actual environment that they will run.

Selenium IDE is not only recording tool: it is a complete IDE. You can choose to use its recording capability, or you may edit your scripts by hand. With autocomplete support and the ability to move commands around quickly, Selenium IDE is the ideal environment for creating Selenium tests no matter what style of tests you prefer.


Features:


  • Easy record and playback
  • Intelligent field selection will use IDs, names, or XPath as needed
  • Autocomplete for all common Selenium commands
  • Walk through tests
  • Debug and set breakpoints
  • Save tests as HTML, Ruby scripts, or any other format
  • Support for Selenium user-extensions.js file
  • Option to automatically assert the title of every page
  • NEW! Easy customization through plugins.


What is Selenium?

Selenium automates browsers. That's it. What you do with that power is entirely up to you. Primarily it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.

Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. It is also the core technology in countless other browser automation tools, APIs and frameworks.

Sunday, December 18, 2011

My Favourite vedios


Sometime its good to remember old technologies rather than leaving them behind.


As always said "Old is Gold". Old gave today gold. In Terms of technology.

My Favourite Vedio

Technology we forgot. Please check whether you remember this or not.