基於selenium Java實現傳感器的批量配置

項目要求通過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");
		
		// login
		driver.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();
			                           
			// 選擇fuQiang
			driver.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 Exception
			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區生育室=====1=================");
			
			// 需等待2秒後再新建
			Thread.sleep(2000);
			
			// 新建傳感器
			driver.findElement(By.xpath(".//*[@id='id_addNew']")).click();
			                           
			// 選擇fuQiang
			driver.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();
			                           
			// 選擇fuQiang
			driver.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();
			                           
			// 選擇fuQiang
			driver.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();
			                           
			// 選擇fuQiang
			driver.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();
		}
	
	}



}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章