Wednesday, June 13, 2012

Webdriver - How to start different browsers.

Webdriver - How to start different browsers.

Following example will help you understand how to invoke different browsers using Webdriver, you can also implemented case structure so that browser selection is automatic instead of comment and uncomment the code.


package MercuryTours;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

//@Listeners({ MercuryTours.TestNG.TestNGCustom.class, MercuryTours.TestNG.TestNGCustomeListener.class })
public class MercuryTours_TC_04 {

private static WebDriver driver;
@BeforeMethod
public static void StartDriver() {

      driver = new InternetExplorerDriver();

/* //Download chromedriver.exe from http://code.google.com/p/chromedriver/downloads/list and place in following location
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe");            
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        driver = new ChromeDriver(options);*/
     
/*    FirefoxProfile Profile = new FirefoxProfile();
        Profile.setPreference("browser.safebrowsing.malware.enabled", false);
        driver = new FirefoxDriver(Profile);*/

//Safari driver still in experimental stage and run only on Mac
//For Windows need to relay on Selenium1 or RC
//Selenium browser = new DefaultSelenium("localhost", 4444, "*safari", "http://www.google.com");

 //driver = new OperaDriver();


}

@AfterMethod(alwaysRun = true)
public void StopDriver() {
driver.quit();
}

@Test(groups = { "MercuryToursTestCases" }, enabled = true,timeOut = 90000)
public void Test_TC_01() throws Exception {
driver.get("http://newtours.demoaut.com/");
Thread.sleep(7000);

}


}

-----------------

7 comments:

  1. Hi Bharath,
    can u help me in updating selenium results in database.

    ReplyDelete
    Replies
    1. Create a vbs that connect to the database. Call .vbs file once each test method is completed. I use a similar technique, but I update in the notepad.

      Delete
  2. Hi Bharath

    I am using Slenium with Jbehave. Can you please let me know how can i run my jbehave scripts on multiple browsers.

    Thanks,
    Sree

    ReplyDelete
  3. hi bharath
    I am using selenium webdriver with java. can you plz tel me. some error message displayed.
    this is my code
    package master2;

    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriverBackedSelenium;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;

    import com.sun.tools.internal.xjc.Driver;


    public class ChromeTest {


    private WebDriverBackedSelenium selenium;

    @Before
    public void setUp() throws Exception {
    System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://65.111.172.80/security/");
    selenium = new WebDriverBackedSelenium(driver,"http://65.111.172.80/security/");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void ChromeTest12() throws Exception {
    driver.get("http://65.111.172.80/security/");
    selenium.type("//*[@id='User_Name']", "shop1");
    selenium.type("//*[@id='Password']", "t1");
    selenium.click("//*[@id='Link']");
    selenium.waitForPageToLoad("30000");
    selenium.click("//*[@id='awesome-menu']/li[5]/a");
    selenium.waitForPageToLoad("70000");
    assert (selenium.isElementPresent("css=input[id*='hdnTimeLogDate']"));
    selenium.click("css=input[value*='day'");
    }

    error meesage displayed

    public void ChromeTest12() throws Exception {

    ReplyDelete
    Replies
    1. What error are you getting?
      Have you copied the above code?

      Delete
  4. import IDE to J UNIT
    error in
    @test
    public void chromtest12() throws Exception{

    underline to the displayed error message (chrometest12)
    so please help me

    ReplyDelete