一.测试内容
1.打开qq音乐网站
2.简单的页面浏览
3.点击歌手
4.点击登录(头像登录)
5.点击第一位歌手
6.点击第四首歌
7.点击播放
8.点击我知道了(小窗提示)
9.点击暂停
10.关闭页面(返回上一页面)
11.点击评论,评论好听
12.搜索一位歌手,断言
二.代码(chrome浏览器)
import java.time.Duration;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
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.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import junit.framework.Assert;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
public class qqtest {
@Test
public void test() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Intel\\chrome\\chrome\\chromedriver.exe");
WebDriver driver =new ChromeDriver();
driver.get("https://y.qq.com");
driver.manage().window().maximize();
Actions action = new Actions(driver);
//点击左测箭头
driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/div[1]/a")).click();
//点击右侧箭头
driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/div[2]/a/i[2]")).click();
driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/div[2]/a")).click();
//点击歌手
driver.findElement(By.xpath("/html/body/div/div/div[1]/div/ul[2]/li[2]/a")).click();
//点击登录
driver.findElement(By.xpath("/html/body/div/div/div[1]/div/div[2]/span/a")).click();
//切换窗口到qq登录
Thread.sleep(2000);
//点击头像登录
driver.switchTo().frame("login_frame");
driver.switchTo().frame("ptlogin_iframe");
driver.findElement(By.xpath("//*[@id=\"img_out_434844939\"]")).click();
Thread.sleep(2000);
//点击第一个歌手
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, 500)");
driver.findElement(By.xpath("//a[@href='/n/ryqq/singer/0025NhlN2yWrP4']")).click();
Thread.sleep(2000);
//第四首歌
driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/div[2]/ul[2]/li[4]/div/div[2]/span/a")).click();
//点击播放
Thread.sleep(2000);
driver.findElement(By.cssSelector(".btn__txt")).click();
Thread.sleep(2000);
//切换页面
Set<String> handles = driver.getWindowHandles();
String oldHandle = driver.getWindowHandle();
String tarHandle = null;
for (String handle : handles) {
if (handle!=oldHandle) {
tarHandle = handle;
} else {
System.out.println("切换失败");
}
}
driver.switchTo().window(tarHandle);
//点击我知道了
WebElement element = driver.findElement(By.xpath("/html/body/div[5]/div/div[2]/div/div[2]/div/div/div[3]/button"));
action.moveToElement(element).click().perform();
Thread.sleep(5000);
关闭当前页面
driver.close();
driver.switchTo().window(oldHandle);
//评论
driver.findElement(By.xpath("//*[@id=\"comment_box\"]/div[2]/div[1]/div[1]/div[1]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[@id=\"comment_box\"]/div[2]/div[1]/div[1]/div[1]")).click();
Thread.sleep(2000)
driver.findElement(By.xpath("//*[@id=\"comment_box\"]/div[2]/div[1]/div[1]/div[2]")).sendKeys("好听");
driver.findElement(By.xpath("//*[@id=\"comment_box\"]/div[2]/div[2]/a")).click();
//返回
driver.navigate().back();
Thread.sleep(2000);
//搜素
driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div/div[1]/div[1]/input")).sendKeys("Don't Be So Serious");
action.sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
//断言
String title = driver.findElement(By.xpath("//*[@id=\"zhida_singer\"]/div/div[1]/h2/a")).getText();
Assert.assertEquals("Low Roar",title);
driver.quit();
}
}