使用Selenium工具的自动化脚本

时间:2019-01-03 13:05:36

标签: java excel selenium-webdriver

我需要有关自动化脚本的帮助。

首先,我需要单击添加按钮,这样它将打开设备ID和设备名称文本框。我正在使用filo将值从excel表格传递到该文本框,然后单击保存。我正在从Excel工作表中读取数据。对于任何现有值,它将一一从excel表中获取数据。但是,如果我在工作表中输入新数据并尝试获取它,则会抛出错误

  

元素不可见

这是我的脚本:

public void Click_Smart_Cart_Button() throws IOException, FilloException, InterruptedException
{

  System.out.println(".....Click operation on Add_Smart_Cart_Device button......");
  WebElement smart_cart_btn=driver.findElement(By.xpath(rx.readdatafromexcel("sheet1", 1,1))); 

  WebDriverWait wait2=new WebDriverWait(driver,15);
 wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(rx.readdatafromexcel("sheet1",1,1))));
  mod.click(smart_cart_btn);
  System.out.println("    ");
  System.out.println("....Button is clicked Successfully....");
  System.out.println("    ");

// Operation to Retrieve data from the Excel Sheet "TestData"

  Fillo fillo= new Fillo();
  Connection con=fillo.getConnection(Filepath);
  String query = "Select SmartCartDeviceID, SmartCartDeviceName from Sheet1";//the parameters name and column name in excel sheet must be same.Eg "username"
  Recordset results= con.executeQuery(query); 


  while(results.next()){

//Click on Device id text box and send data from sheet

 System.out.println("....Click operation on Smart Card Device id text box....... ");
 System.out.println("    ");
 WebElement click_id_box=driver.findElement(By.xpath(rx.readdatafromexcel("sheet1", 2,1)));
 if(click_id_box.isDisplayed()==false)
 {
     driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS); 
     mod.click(smart_cart_btn); 
 }
 mod.click(click_id_box);
 System.out.println("......Smart card device id box is clicked Sucessfully.....");
 System.out.println("   ");
 System.out.println("....Checking whther the id box field is empty or not...");
 System.out.println("  ");

  if(click_id_box != null)
  {
      click_id_box.clear();
  }

click_id_box.sendKeys(results.getField("SmartCartDeviceID"));
 Thread.sleep(1000);
 System.out.print("Device id is "+results.getField("SmartCartDeviceID") +" ");
 System.out.println("    ");
 System.out.println("......Device id is sent succefully from the excel sheet.....");
 System.out.println("  ");

//Click on Device name text box and send data from the sheet

 System.out.println(".....Click operation on Smart Card Device Name Text Box...");
 System.out.println("   ");
 WebElement click_name_box=driver.findElement(By.xpath(rx.readdatafromexcel("sheet1", 3,1)));
 mod.click(click_name_box); 
 System.out.println("......Smart Cart Device id box is clicked Successfully....");
 System.out.println("  ");
 System.out.println("....Checking whther the Device name box field is empty or not.....");
 System.out.println(" ");

 if( click_name_box!=null)
  {
      click_name_box.clear();
  }

 click_name_box.sendKeys(results.getField("SmartCartDeviceName"));
  Thread.sleep(2000);
  System.out.println(" Device Name is "+results.getField("SmartCartDeviceName"));
  System.out.println("  ");
  System.out.println("....Device name is sent successfully from the excel sheet.....");
  System.out.println("   ");

//Click on Save button after sending values to the respective text boxes

  System.out.println("...Click on Save button....");
  WebElement save_btn=driver.findElement(By.xpath(rx.readdatafromexcel("sheet1",4,1)));
  mod.click(save_btn);
  Thread.sleep(2000);
  System.out.println("   ");
  System.out.println("......Save button is clicked successfully.....");


//Handle Confirmation Pop up

  System.out.println("  ");  
  System.out.println(".....After click on save confimation window will appear....");
  System.out.println(" ");
  WebDriverWait wait=new WebDriverWait(driver,30);
  wait.until(ExpectedConditions.elementToBeClickable(By.xpath(rx.readdatafromexcel("sheet1",5,1))));
  System.out.println("....Wait for element to be clickable.....");
  WebElement ok_pop=driver.findElement(By.xpath(rx.readdatafromexcel("sheet1", 5,1)));
  mod.click(ok_pop);
  Thread.sleep(2000);
  System.out.println("  ");
  System.out.println("....Successfully clicked on confirmation ok.....");
  System.out.println("  ");
  System.out.println(".....Successfully completed data retrievation from excel sheet.....");
  System.out.println(" ");
  }   
}
}

readfromexcel是我从excel读取数据的功能。它包含所有webelement的xpath。

0 个答案:

没有答案