Selenium-Java Web自动化测试By和findElemet方法重构-代码重构1

1:By方法的封装

1.1:未封装前的方法

public class BeforeBean {
	
	private WebDriver driver;
	
	@Test
	public void WebTest(){
//		初始化浏览器
		driver = new FirefoxDriver();	
		driver.get("https://www.csdn.net/");
//		最大化浏览器
		driver.manage().window().maximize();
//		获取登录框
		driver.findElement(By.linkText("登录")).click();
//		点击账号登录
		driver.findElement(By.linkText("linkText")).click();
//		输入账号
		driver.findElement(By.id("username")).sendKeys("你的账号");
//		输入密码
		driver.findElement(By.id("password")).sendKeys("你的密号");
//		点击登录
		driver.findElement(By.xpath(".//*[@id='fm1']/input[8")).click();
	}
}

1.2:封装By和findElement findElements方法后

By方法封装的好处是 增强了可操作性 我们改变元素的定位方式的时候不用改代码 增强了可操作性。即无论Tester使用哪一种定位方式 只用告诉程序你使用的定位方式即可 不用再去更改代码。

public class testDemos {
	
	private WebDriver driver;
	
	@Test
	public void testDemo() throws Exception{
		
		driver = new FirefoxDriver();
		
		driver.get("https://www.csdn.net/");
		
		driver.manage().window().maximize();
		
		Thread.sleep(3000);
		
//		寻找登录按钮  弹出登录框
		this.elementBean("linkText", "登录").click();	
		
//		使用账号登录
		this.elementBean("linkText", "账号登录").click();	

//		输入账号
		this.elementBean("id", "username").sendKeys("你的账号");	
		
//		输入密码
		this.elementBean("xpath", ".//*[@id='password']").sendKeys("你的密码");	
		
//		点击登录进行登录操作
		this.elementBean("xpath", ".//*[@id='fm1']/input[8]").click();	
	
	}
	
	/**
	 * 封装by
	 * */
	
	public By bystr(String LocateMethon, String LocatElement){
		
		if(LocateMethon.equalsIgnoreCase("id")){
			
			return By.id(LocatElement);
		}else if(LocateMethon.equalsIgnoreCase("name")){
			
			return By.name(LocatElement);
		}else if(LocateMethon.equalsIgnoreCase("tagNmae")){
			
			return By.tagName(LocatElement);
		}else if(LocateMethon.equalsIgnoreCase("linkText")){
			
			return By.linkText(LocatElement);
		}else if(LocateMethon.equalsIgnoreCase("xpath")){
			
			return By.xpath(LocatElement);
		}else if(LocateMethon.equalsIgnoreCase("cssSelector")){
			
			return By.cssSelector(LocatElement);
		}else{
			
			return By.partialLinkText(LocatElement);
		}
		
	}
	
	/**
	 * 封装Element
	 * */
	
	public WebElement elementBean(String LocateMethon, String LocatElement){
	
		return driver.findElement(this.bystr(LocateMethon, LocatElement));
	}
	
	/**
	 * 封装Elements集合
	 * */
	
	public List<WebElement> elementList(String LocateMethon, String LocatElement){
	
		return driver.findElements(this.bystr(LocateMethon, LocatElement));
	}
}

1.3:另外一种重构findElement和findElements的方法

public class testDemos {
	
	private WebDriver driver;
	
	@Test
	public void testDemo() throws Exception{
		
		driver = new FirefoxDriver();
		
		driver.get("https://www.csdn.net/");
		
		driver.manage().window().maximize();
		
		Thread.sleep(3000);
		
//		寻找登录按钮  弹出登录框
		this.elementBean(this.bystr("linkText", "登录")).click();	
		
//		使用账号登录
		this.elementBean(this.bystr("linkText", "账号登录")).click();	

//		输入账号
		this.elementBean(this.bystr("id", "username")).sendKeys("你的账号");	
		
//		输入密码
		this.elementBean(this.bystr("xpath", ".//*[@id='password']")).sendKeys("你的密码");	
		
//		点击登录进行登录操作
		this.elementBean(this.bystr("xpath", ".//*[@id='fm1']/input[8]")).click();	
	
	}
	
	/**
	 * 封装by
	 * */
	
	public By bystr(String LocateMethon, String LocatElement){
		
		if(LocateMethon.equalsIgnoreCase("id")){
			
			return By.id(LocatElement);
		}else if(LocateMethon.equalsIgnoreCase("name")){
			
			return By.name(LocatElement);
		}else if(LocateMethon.equalsIgnoreCase("tagNmae")){
			
			return By.tagName(LocatElement);
		}else if(LocateMethon.equalsIgnoreCase("linkText")){
			
			return By.linkText(LocatElement);
		}else if(LocateMethon.equalsIgnoreCase("xpath")){
			
			return By.xpath(LocatElement);
		}else if(LocateMethon.equalsIgnoreCase("cssSelector")){
			
			return By.cssSelector(LocatElement);
		}else{
			
			return By.partialLinkText(LocatElement);
		}
		
	}
	
	/**
	 * 封装Element
	 * */
	
	public WebElement elementBean(By by){
	
		return driver.findElement(by);
	}
	
	/**
	 * 封装Elements集合
	 * */
	
	public List<WebElement> elementList(By by){
	
		return driver.findElements(by);
	}
}

猜你喜欢

转载自blog.csdn.net/hujyhfwfh2/article/details/81051071