基于selenium Java实现传感器的批量配置

来源:互联网 发布:嵌入式网络视频服务器 编辑:程序博客网 时间:2024/06/05 09:12

项目要求通过TeamViewer远程部署GCPI项目,其中的重要一步是配置传感器,由于传感器数量众多,大约2,3000个传感器,同事手动配置两天后拉我去帮忙,随发现手动重复性太高。最近正好在研究selenium的界面测试,在本机上尝试后,发现可以成功配置一个传感器,于是远程到TeamViewer上开始搭建selenium环境(安装jdk7,配置环境变量,安装MyEclipse,加载selenium jar包),然后开始用java根据需要部署的项目编写selenium配置传感器的脚本,完成后,极大提高效率。


package selenium1;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.Select;public class GcpiTeamViewer {public static void main(String[] args) throws InterruptedException {System.out.println("Login GCPI");WebDriver driver = new FirefoxDriver();driver.get("http://192.168.28.56:8100/GCPI/0-webapp/html/factory_config.html#/sensor");// logindriver.findElement(By.cssSelector(".container-section-inputOne.container-section-input."+ "ng-pristine.ng-untouched.ng-valid.ng-valid-maxlength")).clear();driver.findElement(By.cssSelector(".container-section-inputOne.container-section-input."+ "ng-pristine.ng-untouched.ng-valid.ng-valid-maxlength")).sendKeys("aa");driver.findElement(By.cssSelector(".container-section-inputTwo.container-section-input."+ "ng-pristine.ng-untouched.ng-valid")).clear();driver.findElement(By.cssSelector(".container-section-inputTwo.container-section-input."+ "ng-pristine.ng-untouched.ng-valid")).sendKeys("123456");driver.findElement(By.cssSelector(".container-section-inputThree")).click();Thread.sleep(2000);// 传感器配置driver.findElement(By.xpath(".//*[@id='sensorId']")).click();//Thread.sleep(2000);           //窗口最大化driver.manage().window().maximize();Thread.sleep(2000);// for循环用来实现字符串拼接,实现工厂名称的自加for(int i=11; i<=19; i++){String s1 = "F区生育室";String s2 = s1+i;System.out.println("=============="+s2+"====================");// 新建传感器driver.findElement(By.xpath(".//*[@id='id_addNew']")).click();                           // 选择fuQiangdriver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[1]/div[4]/span")).click();//选择工厂Select userSelect1=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[1]/select")));  userSelect1.selectByVisibleText(s2);                // 选择传感器名称driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[2]/input")).sendKeys("冷风机运行");// 选择PLC地址driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[3]/input")).sendKeys("1");// 选择传感器种类Select userSelect2=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[2]/div[1]/select")));  userSelect2.selectByVisibleText("温度");  // 状态地址driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[3]/div[1]/input")).sendKeys("0");// 最高报警值driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[5]/div[1]/input")).sendKeys("100");// 最低报警值driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[5]/div[2]/input")).sendKeys("0");// 计算表达式driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[6]/div[1]/input")).sendKeys("1*x");// modbus配置Select userSelect3=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[6]/div[2]/select")));  userSelect3.selectByVisibleText(s2);  // 点击“确定”driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[6]/span[1]")).click();// 弹框:新建成功,点击“确认”// div模拟的alert弹框,并非使用Alert类,直接通过xpath定位“确认”按钮// 此处务必记得先sleep 2秒,否则会报错无法识别弹框:No Alert Present ExceptionThread.sleep(2000); driver.findElement(By.xpath("html/body/common-showalerttxtbox/div/div/div/ul[2]/li/span")).click();// 刷新driver.navigate().refresh();System.out.println("===================F区生育室=====1=================");// 需等待2秒后再新建Thread.sleep(2000);// 新建传感器driver.findElement(By.xpath(".//*[@id='id_addNew']")).click();                           // 选择fuQiangdriver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[1]/div[4]/span")).click();Select userSelect1b=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[1]/select")));  userSelect1b.selectByVisibleText(s2);                driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[2]/input")).sendKeys("新风机运行");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[3]/input")).sendKeys("1");Select userSelect2b=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[2]/div[1]/select")));  userSelect2b.selectByVisibleText("温度");  // 状态地址driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[3]/div[1]/input")).sendKeys("1");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[5]/div[1]/input")).sendKeys("100");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[5]/div[2]/input")).sendKeys("0");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[6]/div[1]/input")).sendKeys("1*x");Select userSelect3b=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[6]/div[2]/select")));  userSelect3b.selectByVisibleText(s2);  //Thread.sleep(5000);driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[6]/span[1]")).click();//新建成功Thread.sleep(2000);driver.findElement(By.xpath("html/body/common-showalerttxtbox/div/div/div/ul[2]/li/span")).click();// 刷新driver.navigate().refresh();System.out.println("===================F区生育室=====2=================");Thread.sleep(2000);// 新建传感器driver.findElement(By.xpath(".//*[@id='id_addNew']")).click();                           // 选择fuQiangdriver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[1]/div[4]/span")).click();Select userSelect1c=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[1]/select")));  userSelect1c.selectByVisibleText(s2);                driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[2]/input")).sendKeys("加湿器1运行");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[3]/input")).sendKeys("1");Select userSelect2c=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[2]/div[1]/select")));  userSelect2c.selectByVisibleText("温度");  // 状态地址driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[3]/div[1]/input")).sendKeys("2");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[5]/div[1]/input")).sendKeys("100");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[5]/div[2]/input")).sendKeys("0");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[6]/div[1]/input")).sendKeys("1*x");Select userSelect3c=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[6]/div[2]/select")));  userSelect3c.selectByVisibleText(s2);  //Thread.sleep(5000);driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[6]/span[1]")).click();//新建成功Thread.sleep(2000);driver.findElement(By.xpath("html/body/common-showalerttxtbox/div/div/div/ul[2]/li/span")).click();// 刷新driver.navigate().refresh();System.out.println("===================F区生育室=====3=================");Thread.sleep(2000);// 新建传感器driver.findElement(By.xpath(".//*[@id='id_addNew']")).click();                           // 选择fuQiangdriver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[1]/div[4]/span")).click();Select userSelect1c1=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[1]/select")));  userSelect1c1.selectByVisibleText(s2);                driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[2]/input")).sendKeys("加湿器2运行");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[3]/input")).sendKeys("1");Select userSelect2c1=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[2]/div[1]/select")));  userSelect2c1.selectByVisibleText("温度");  // 状态地址driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[3]/div[1]/input")).sendKeys("3");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[5]/div[1]/input")).sendKeys("100");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[5]/div[2]/input")).sendKeys("0");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[6]/div[1]/input")).sendKeys("1*x");Select userSelect3c1=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[6]/div[2]/select")));  userSelect3c1.selectByVisibleText(s2);  //Thread.sleep(5000);driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[6]/span[1]")).click();//新建成功Thread.sleep(2000);driver.findElement(By.xpath("html/body/common-showalerttxtbox/div/div/div/ul[2]/li/span")).click();// 刷新driver.navigate().refresh();Thread.sleep(2000);System.out.println("===================F区生育室=====4=================");// 新建传感器driver.findElement(By.xpath(".//*[@id='id_addNew']")).click();                           // 选择fuQiangdriver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[1]/div[4]/span")).click();Select userSelect1c2=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[1]/select")));  userSelect1c2.selectByVisibleText(s2);                driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[2]/input")).sendKeys("机组状态");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[1]/div[3]/input")).sendKeys("1");Select userSelect2c2=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[2]/div[1]/select")));  userSelect2c2.selectByVisibleText("温度");  // 状态地址driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[3]/div[1]/input")).sendKeys("8");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[5]/div[1]/input")).sendKeys("100");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[5]/div[2]/input")).sendKeys("0");driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[6]/div[1]/input")).sendKeys("1*x");Select userSelect3c2=new Select(driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[4]/div[6]/div[2]/select")));  userSelect3c2.selectByVisibleText(s2);  //Thread.sleep(5000);driver.findElement(By.xpath(".//*[@id='fixed_left_id']/div/common-sensoradd/div[1]/div/div/div[6]/span[1]")).click();// 新建成功,我知道了//driver.findElement(By.xpath("html/body/common-showalerttxtbox/div/div/div/ul[2]/li/span")).click();//driver.findElement(By.cssSelector(".commonAlert>div>ul>li>span[1]")).click();//driver.switchTo().alert().accept_alert(expected_text=None, text_to_write=None);//WebDriverWait wait = new WebDriverWait(driver, 5);//Alert alert = wait.until(ExpectedConditions.alertIsPresent());//alert.accept();//新建成功Thread.sleep(2000);driver.findElement(By.xpath("html/body/common-showalerttxtbox/div/div/div/ul[2]/li/span")).click();// 刷新driver.navigate().refresh();Thread.sleep(2000);System.out.println("===================F区生育室=====5=================");//driver.quit();}}}

原创粉丝点击