Java实现简单的selenium开启多个浏览器(Paramaters实现)

XML配置

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="photo suite" parallel="tests" thread-count="2">
	<test name="photo test1">
    <parameter name="browser" value="chrome" />
        <classes>
			<class name = "demo.TestReorder2"/>
        </classes>
    </test>
    
    <test name="photo test2">
    <parameter name="browser" value="firefox" />
        <classes>
			<class name = "demo.TestReorder2"/>
        </classes>
    </test>
</suite>

TestReorder2.java

package demo;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import demo.login;

public class TestReorder2 {
	static WebDriver driver;
	
	@Parameters("browser")
	@BeforeClass
	public void beforeclass(String browserName) throws InterruptedException {
		
		driver = login.main(browserName);
		login.into(driver);
	
	}
	
	@Test
	public static void ReorderTest() throws InterruptedException {
		//点击排序按钮
		driver.findElement(By.xpath("//span[contains(text(),'排序')]")).click();
		
		//点击自定义排序
		driver.findElement(By.xpath("//a[contains(text(),'自定义排序')]")).click();
		
		WebElement source = driver.findElement(By.xpath("//div[@title='排序B']"));
		WebElement target = driver.findElement(By.xpath("//div[@title='排序A']"));
		Actions act = new Actions(driver);
		act.dragAndDrop(source, target).perform();
		
		Thread.sleep(2000);
		
		//点击保存按钮
		driver.findElement(By.xpath("//a[contains(text(),'保存')]")).click();
		
		//排序验证,变成B-A-C
		List<WebElement> albums = driver.findElements(By.xpath("//*[@id=\"photo-163-com-container\"]/div[1]/div[2]/div/div[2]/span[1]"));

		for( int i = 0 ; i < albums.size() ; i++) {
			if(albums.get(i).getText().equals("排序B")) {
				Assert.assertEquals(albums.get(i+1).getText(), "排序A");
				Assert.assertEquals(albums.get(i+2).getText(), "排序C");
				break;
			}
		}

	}
	
	@AfterMethod
	public void afterMethod() throws InterruptedException {
		//数据清理-重新排序
		//点击排序按钮
		driver.findElement(By.xpath("//span[contains(text(),'排序')]")).click();
		
		//点击自定义排序
		driver.findElement(By.xpath("//a[contains(text(),'自定义排序')]")).click();
		WebElement source = driver.findElement(By.xpath("//div[@title='排序A']"));
		WebElement target = driver.findElement(By.xpath("//div[@title='排序B']"));
		Actions act = new Actions(driver);
		act.dragAndDrop(source, target).perform();
		driver.findElement(By.xpath("//a[contains(text(),'保存')]")).click();
	}
	
	@AfterClass(alwaysRun = true)
	public void afterClass() {
		driver.quit();
	}
}

login.java

package demo;

import java.util.concurrent.TimeUnit;

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.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class login {
	
	
	@Test
	public static WebDriver main(String browserName) throws InterruptedException {
		// TODO Auto-generated method stub
		WebDriver driver = null;
		
		if (browserName.equalsIgnoreCase("firefox")) {
			System.setProperty("webdriver.firefox.bin", "D:\\Mozilla Firefox\\firefox.exe");
			driver = new FirefoxDriver();
		}else{
			driver = new ChromeDriver();
		}
		
		driver.manage().window().maximize();
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		
		driver.get("http://photo.163.com");
		
		//切换frame
		driver.switchTo().frame(driver.findElement(By.xpath(("//*[@id=\"ursloginwrap\"]/iframe"))));
		
		driver.switchTo().frame(driver.findElement(By.xpath(("//*[@id=\"login-URS-iframe\"]/iframe"))));
		
		//输入用户名
		WebElement userName = driver.findElement(By.name("email"));
		userName.sendKeys("XXXX");
		
		//输入密码
		WebElement password = driver.findElement(By.name("password"));
		password.sendKeys("XXXX");
		
		//点击登录
		driver.findElement(By.id("dologin")).click();
		
		Thread.sleep(5000);
		
		//个人账号会进入博客激活界面,试了下服务已从8月分开始关闭了,我这里重新get或者浏览器回退达到相同的目的
		driver.switchTo().defaultContent();		//加这句以提高稳定性,防止报错can't access dead object
		driver.get("http://photo.163.com");
		
		return driver;
	}
	
	public static void into(WebDriver driver) throws InterruptedException {
		//点击进入我的相册按钮
		driver.findElement(By.xpath("//*[@id='J-loginMod']//span[@class='name']")).click();
		
		Thread.sleep(5000);
		
		//判断是否进入我的相册
		Assert.assertEquals(driver.getTitle(), "evanqianyue的网易相册_evanqianyue个人相册相片存储_网易相册", "进入我的相册错误");
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq544649790/article/details/84571776