Wednesday, December 5, 2012

Comebine Selenium 2.0 + TestNG + Junit


CLASS for Selenium 2.0 such as:
//Build class data
Public class data {
       public static final WebDriver oWebDriver = new FirefoxDriver();
       public  static final String urllogin = "https://accounts.google.com/ServiceLogin?hl=vi&continue=https://www.google.com.vn/";
      
       public static final String idemail1 ="";
       public static final String  txtemail = "";
       public static  final String idpass1 = "";
       public static final String txtpass = "";
       public static final String btnlogin = "";
}

//build function
public class Login {
      
       public void login(WebDriver driver){
              driver = data.oWebDriver;
              driver.get(Data.data.urllogin);
             
             
                           // Enter Email
                           WebElement email = driver.findElement(By.xpath(Data.data.idemail1));
                           email.sendKeys(Data.data.txtemail);
                           //Enter password
                           WebElement pass = driver.findElement(By.xpath(Data.data.idpass1));
                           pass.sendKeys(Data.data.txtpass);
                           //Click Login
                           WebElement btnlogin = driver.findElement(By.xpath(Data.data.btnlogin));
                           btnlogin.click();
             
                     }
}

//Call function to use Junit
public class loadgoogle extends TestCase{
      
       public WebDriver driver;
      
       private int TIMEOUT_THREE_HOURS;

       @BeforeTest(alwaysRun=true)
       public void setUp() throws Exception {
              Logger Log;
              Log = Logger.getLogger(gogole.class);
             
       PropertyConfigurator.configure("log4j.properties");
              driver = data.oWebDriver;
              driver.get(Data.data.urllogin);  
       }

       @Test(groups="gogole")
       public void testcase() throws Exception{
              Login in = new Login();
              in.login(data.oWebDriver);
              driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
              Logout out = new Logout();
              out.logout(data.oWebDriver);
              in.screenshotJunit();
       }
      
       @AfterTest(alwaysRun=true)
       public void tearDown() throws Exception {
              //driver.close();
                System.out.println("closing the FF");
                                             try {
                                                     driver.quit();
                                             }catch(Exception e) {
                                                     System.out.println("Driver quit failed with error" + e);
                                             }
       }


}

//Build testng.xml such as:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
       <listeners>
              <listener class-name="com.utility.Screenshot"></listener>
       </listeners>

<test name="loadgoogle" preserve-order="true">
              <groups>
                  <run>
                     <include name="gogole"/>
                  </run>
              </groups>
      
<test>
              <classes>
           <class name="Selenium Testcase.loadgoogle">
               <methods>
                   <include name="testcase"></include>
                      <exclude name="testcaselogout"></exclude>
               </methods>
           </class>
       </classes>
      
</test>>     
</suite>

1 comment:

  1. BUILD FAILED
    D:\ECLIPSE\workspace_eclipseclassic\seleniumdriver\build.xml:34: Unable to find a javac compiler;
    com.sun.tools.javac.Main is not on the classpath.
    Perhaps JAVA_HOME does not point to the JDK.
    It is currently set to "C:\Program Files\Java\jre6"
    ==>
    1) In Eclipse click Run->External Tools->External Tools Configurations
    2) Click JRE tab
    3) Click Installed JREs... button
    4) Click Add button (select Standard VM, where applicable)
    5) Click Directory Button
    6) Browse to your JDK version (not JRE) of your installed Java (eg: C:\Program Files\Java\jdk1.7.0_04)
    7) Click Finish.
    8) Re-run Ant script - have fun!

    ReplyDelete