1. 程式人生 > >Java實現簡單的selenium開啟多個瀏覽器(Paramaters實現)

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個人相簿相片儲存_網易相簿", "進入我的相簿錯誤");
	}
	
}