简体   繁体   中英

Java Selenium webdriver code not execute ;Password Field remains Empty

I try to execute java webdriver code ,selenium test for login page .I passed input from excel it correctly assign values to username , password .once click login button -

 Till login button submit works perfectly. if once login button clicked.. password field in that page remain empty and login attempt failed i cant sort out the problem.

Thank you all Your reply and appreciate

package exceltest;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;



public class sample {

    public static void main( String[] args) throws Exception {
        String [][] data;
        data = excelread();

        String expectedtitle;
        for (int i = 1; i < data.length; i++ )
        {

        expectedtitle = login(data[i][0],data[i][1]); 

        System.out.println("page title after login is" + expectedtitle );

        if(expectedtitle.equalsIgnoreCase("homepage Title"))
        {

            System.out.println("PASSED");
        }

        else{
            System.out.println("FAILED");

            }

        }
    }







    public static String login(String username,String password) throws InterruptedException {
         WebDriver driver = new FirefoxDriver();

         driver.get("http://localhost/xxx/Default.aspx");

         Thread.sleep(1000);
          //driver.findElement(By.id("LoginUserName")).clear();
            driver.findElement(By.id("LoginUserName")).sendKeys(username);
           //driver.findElement(By.id("LoginPassword")).clear();


            driver.findElement(By.id("LoginPassword")).sendKeys(password);
            driver.findElement(By.id("LoginLoginButton")).click();
            Thread.sleep(1000); 
            String expectedtitle = driver.getTitle();
            return expectedtitle;

          }








    public static  String [][] excelread()throws Exception
     {
         File excel = new File("D:\\Book1.xlsx");
         FileInputStream fis = new FileInputStream(excel);
         XSSFWorkbook wb = new XSSFWorkbook(fis);
         XSSFSheet ws = wb.getSheet("Sheet1");

         int totrow = ws.getLastRowNum()+ 1;
         int totcol = ws.getRow(0).getLastCellNum();
         String [][] data = new String[totrow][totcol];

         for (int i = 0 ; i < totrow ; i++) {
             XSSFRow row = ws.getRow(i);
             for (int j=0  ; j < totcol ; j++){
             XSSFCell cell = row.getCell(j);
             String value = cellToString(cell);
             data[i][j] = value;
              System.out.println("The value is  "   + value);



             } 
     }
         return data;
         }


     public static String cellToString(XSSFCell cell) {
         int type;
         Object result ;
         type = cell.getCellType();
         switch (type) {
         case 0 :
         result = cell.getNumericCellValue();
         break;
         case 1 :
         result = cell.getStringCellValue();
         break;
         default :
         throw new RuntimeException("There are no support for this type of cell");
         }
         return result.toString();
         }
     }

You are not using the same locator (class 'By') for clearing the password field and to enter the passsword. Are you sure both locators are correct? Your password filed is empty because you are not selecting the right element. You must select the input tag of your html.

Other remarks. You shouldn't use thread.sleep() to make your test case works, you will have race condition. You should keep a reference to the elements. Calling findElement everytime is useless and will make your test slow.

WebElement e = driver.findElement(By.id("whatever"));
e.clear();
e.sendKeys(...);

答案就在这里...我将Webdriver代码与java eclipse一起使用,.net Web应用程序的自动登录。“ xxx.aspx.cs”页面中提供的默认密码无法执行..原因:该页面具有如上功能的情况提交点击,它将登录请求发送到服务器并清除密码字段

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM