Monday, November 5, 2012

Example for Selenium 2.0



//Using IE
              System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
                WebDriver oWebDriver = new InternetExplorerDriver();
                oWebDriver.get("https://www.dienmay.com/tai-khoan/dang-ky");
             
             
              //Using Firefox
              //final String sUrl = "https://www.dienmay.com/tai-khoan/dang-ky";
              //WebDriver oWebDriver = new FirefoxDriver();
             
              File file = new File("D:\\ECLIPSE\\workspace_eclipseclassic\\test.xls");
              //Reading data in Excel file
              Workbook wb = Workbook.getWorkbook(file);
              jxl.Sheet sheet = wb.getSheet(0) ;
              int rows = sheet.getRows(); 
             
              String filename="D:\\ECLIPSE\\workspace_eclipseclassic\\result.xls" ;
              //Create sheet
              HSSFWorkbook hwb = new HSSFWorkbook();
              HSSFSheet sheet1 =  ((HSSFWorkbook) hwb).createSheet("Result");
              //Create row
              HSSFRow row1=   sheet1.createRow(0);
              for(int row = 0; row < rows; row++){
              //     oWebDriver.get(sUrl);
              //     oWebDriver.get("");
                    
                     //Reading data from excel file to write element Email fields
                     WebElement email = oWebDriver.findElement(By.id("txtEmail"));
                     email.clear();
                     String eemail = sheet.getCell(0, row).getContents();
                     email.sendKeys(eemail);
                    
WebElement phone =  oWebDriver.findElement(By.xpath("//input[@id='txtMobile']"));
                     phone.clear();
                     String pphone = sheet.getCell(1, row).getContents();
                     phone.sendKeys(pphone);
                    
WebElement password = oWebDriver.findElement(By.xpath("//input[@id='txtPassword']"));
                     password.clear();
                     String ppassword = sheet.getCell(2, row).getContents();
                     password.sendKeys(ppassword);
                    
new Select(oWebDriver.findElement(By.id("ddlProvince"))).selectByVisibleText("TP.Hồ Chí Minh");  
WebElement button = oWebDriver.findElement(By.xpath("//input[@id='btnSubmit']"));
                     button.click();
                    
                     Thread.sleep(10000);
WebElement test = oWebDriver.findElement(By.xpath("//*[@id='form1']/div[2]/div[2]/div[1]"));
                     //Compare result callback
                     if(test.isDisplayed()){                               
                           System.out.println("Pass");
                           writeresult("Pass");
                           row1.createCell(row).setCellValue("Pass");
                     }
                     else{
                           System.out.println("Fail");
                           writeresult("Fail");
                           row1.createCell(row).setCellValue("Fail");
                     }
              }
              //Write excel file
              FileOutputStream fileOut =  new FileOutputStream(filename);
              hwb.write(fileOut);
              fileOut.close();
              System.out.println("Your excel file has been generated!");
              oWebDriver.quit();  
              }
  

              public static void writeresult(String result) throws IOException{
              ByteArrayOutputStream f = new ByteArrayOutputStream();
              byte buf[] = result.getBytes();
              f.write(buf);
              OutputStream f2 = new FileOutputStream("output.xls");
               f.writeTo(f2);
               f2.close();
              f.close();   
               }

1 comment:

  1. If you have met such as:
    + Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
    ==> Pls library selenium-server-standalone-version.jar
    + Selenium RC:
    Could not contact Selenium Server; have you started it on 'localhost:4444' ?
    [testng] Read more at http://seleniumhq.org/projects/remote-control/not-started.html
    [testng] Connection refused: connect
    You run server such as:
    cmd->d:
    cd D:\Testing\Selenium\thinh-selenium\selenium_server
    D:\Testing\Selenium\thinh-selenium\selenium_server>java -jar selenium-server-standalone-2.21.0.jar

    + Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"xpath","selector":"//html/body/div[2]/div/div[3]/form/div/div/input"}
    Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:09:54'
    System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_24'
    Using: driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    ReplyDelete