-1
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import utils.ReadExcelFile;

public class TestUser {
public static void main(String[] args) throws InvalidFormatException, InterruptedException
{
    String driverPath = System.getProperty("user.dir")+"/DriverFiles/chromedriver.exe";
    System.setProperty("webdriver.chrome.driver", driverPath); 
    WebDriver driver=new ChromeDriver();
    driver.manage().window().maximize();         
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    ReadExcelFile xllib= new ReadExcelFile();
    String url = xllib.getExcelData("Login",1,0);
    driver.get(url); //Enter the url in the browser;
    String userName = xllib.getExcelData("Login",1,1);
    driver.findElement(By.id("userName")).sendKeys(userName); //Enter user name;
    String password = xllib.getExcelData("Login",1,2);
    driver.findElement(By.id("password")).sendKeys(password); //Enter password;
    driver.findElement(By.id("submit")).click();  //Click on login button;
    driver.findElement(By.xpath("//*[@id='main_nav']/ul/li[9]/a/span[2]")).click(); //Click on Administration tab;
    // driver.findElement(By.xpath("//*[@id='ALL_CONTENT_ID']/div[2]/div[1]/div[2]/ul/li[1]/a")).click(); //Click on User tab in Administration ;
    driver.findElement(By.xpath("//*[@id='page_content']/div[1]/div[2]/a[2]/i")).click(); //Click on Add User;
    String firstName = xllib.getExcelData("AddUser",1,3);
    driver.findElement(By.id("firstName")).sendKeys(firstName);
    String lastName = xllib.getExcelData("AddUser",1,4);
    driver.findElement(By.id("lastName")).sendKeys(lastName);
    String userId = xllib.getExcelData("AddUser",1,5);
    driver.findElement(By.id("userName")).sendKeys(userId);
    String emailId = xllib.getExcelData("AddUser",1,6);
    driver.findElement(By.id("emailId")).sendKeys(emailId); 
    String phoneNo = xllib.getExcelData("AddUser",1,7);
    driver.findElement(By.id("phoneNo")).sendKeys(phoneNo); 
    Thread.sleep(2000); 
    String password1 = xllib.getExcelData("AddUser",1,8);
    driver.findElement(By.id("password")).sendKeys(password1);  
    String confirmPassword = xllib.getExcelData("AddUser",1,9);
    driver.findElement(By.id("confirmPassword")).sendKeys(confirmPassword); 
    driver.findElement(By.xpath("//*[@id='entityId_ms']")).click(); //Click on Entity drop down;
    driver.findElement(By.xpath("//*[@id='ALL_CONTENT_ID']/div[5]/ul/li[2]/label/span")).click(); //Select pridction check box;
    driver.findElement(By.xpath("//*[@id='entityId_ms']")).click(); //close the  Entity drop down
    String filePath = System.getProperty("user.dir")+"/UploadFiles/Admin_M.jpg"; //Select upload image icon;
    driver.findElement(By.id("file")).sendKeys(filePath); //Select uploading file;
    driver.findElement(By.xpath("//*[@id='userDetailsForm']/div[4]/div/div/div/label")).click(); //click on All View Access;
    driver.findElement(By.xpath("//*[@id='userDetailsForm']/div[5]/div/div/div/div/label[1]")).click(); //Select role as admin;
    driver.findElement(By.xpath("//*[@id='userDetailsForm']/div[6]/input")).click();   //Click on create button; 
    driver.findElement(By.xpath("//*[@id='ALL_CONTENT_ID']/div[5]/div[3]/div/button")).click(); //Click on Ok button in the popup message;
    driver.quit();
            }   
}

1 Answer 1

1

You can add a forloop for iterating the code multiple times.

    for(i=0;i<rownum;i++){
      driver.get(url); //Enter the url in the browser;
      String userName = xllib.getExcelData("Login",i,1);
      driver.findElement(By.id("userName")).sendKeys(userName); //Enter user name;
      String password = xllib.getExcelData("Login",i,2);
      //your remaining code.....
    }

The code will iterate for the number of rows in the excel sheet. Check if this helps.

4
  • 1
    How do you get rownum?
    – Alexey R.
    Jun 1, 2018 at 9:32
  • @AlexeyR. how about int rownum = xllib.getLastRowNum(); ??
    – Rishikesh
    Jun 1, 2018 at 10:39
  • I do not know. If I knew I wouldn't ask.
    – Alexey R.
    Jun 1, 2018 at 11:03
  • @KetanDeshpande It will be better to use XSSF or HSSF libraries for reading/writing from/to excel files...
    – Rishikesh
    Jun 1, 2018 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.