云音乐系统评论模块介绍
主要包括:评论发表,评论回复,评论点赞和取消点赞,评论删除,评论查看(精彩评论和最新评论)。
![](https://img.haomeiwen.com/i10027900/960d1fde4aa0a709.png)
![](https://img.haomeiwen.com/i10027900/cc53010f56403f70.png)
评论模块自动化测试的总体思路
1)设计评论发表的测试点
2)设计评论回复的测试点
3)设计评论点赞和取消点赞的测试点
4)设计删除评论的测试点
5)设计评论查看的测试点
6)设计评论发表的Selenium自动化脚本并调试
7)设计评论回复的Selenium自动化脚本并调试
8)设计评论点赞和取消点赞的Selenium自动化脚本并调试
9)设计评论删除的Selenium自动化脚本并调试
10)设计评论查看的Selenium自动化脚本并调试
11)创建TestNG测试集(将需要运行的测试脚本添加进来)
12)运行TestNG测试集
评论发表的测试点(测试需求)
1)输入的内容是合法的,发表评论成功
2)输入的内容包含表情,发表评论成功
3)输入的内容包含@{账号},发表评论成功
4)输入的内容包含换行符,发表评论成功
5)输入的内容为140个中文字符或者70个表情,发表评论成功
6)输入的内容为1个英文字母,发表评论成功
7)输入的内容包含HTML代码,发表评论成功
8)输入的内容包含JS代码,发表评论成功
9)输入的内容包含QQ号码,发表评论失败
10)输入的内容包含手机号码,发表评论失败
11)输入的内容包含特殊字符,发表评论失败
12)输入的内容过长,发表评论失败
13)输入的内容是空,发表评论失败
14)输入的内容是空格,发表评论失败
15)针对同一首歌,频繁评论会被系统拦截
16)输入的内容包含敏感词汇,发表评论失败
17)非登录状态,不能评论
评论发表的第1个测试脚本
代码如下:
package examples;
import java.util.concurrent.*;
import org.openqa.selenium.*;
import org.openqa.selenium.ie.*;
import static org.testng.Assert.*;
import org.testng.annotations.*;
public class MusicCloudTest {
private WebDriver driver;
@DataProvider
public Object[][] data1(){
Object[][] arr = {
{"非登录状态,发表评论失败","https://music.163.com/#/song?id=29099236",By.className("area"),By.className("n-log2"),"手机号登录"}
};
return arr;
}
@Test(dataProvider="data1")
public void f1(String desc,String url,By by1,By by2,String expected) throws Exception{
driver.get(url);
//Thread.sleep(2000);
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[1]")));
//点击评论选项
driver.findElement(By.className("u-btni-cmmt")).sendKeys(Keys.ENTER);
Thread.sleep(2000);
//driver.findElement(by1).click();
driver.switchTo().defaultContent();
assertTrue(driver.findElement(by2).getText().contains(expected), desc);
}
@BeforeTest
public void beforeTest() {
//设置IE浏览器驱动的路径
System.setProperty("webdriver.ie.driver", "d:\\drivers\\IEDriverServer.exe");
//InternetExplorerOptions option = new InternetExplorerOptions();
//option.requireWindowFocus();
//driver = new InternetExplorerDriver(option);
driver = new InternetExplorerDriver();
//System.setProperty("webdriver.gecko.driver", "d:\\drivers\\geckodriver.exe");
//driver = new FirefoxDriver();
//设置默认的等待时长
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//最大化浏览器窗口
//driver.manage().window().maximize();
}
@AfterTest
public void afterTest() {
driver.quit();
}
}
评论发表的第2个测试脚本
代码如下:
package examples;
import static org.testng.Assert.*;
import java.util.*;
import java.util.concurrent.*;
import org.openqa.selenium.*;
import org.openqa.selenium.ie.*;
import org.testng.annotations.*;
public class MusicCloudTest2 {
private WebDriver driver;
@DataProvider(parallel=false)
public Object[][] data2() {
Object[][] arr = {
{ "输入的内容是HTML代码", "https://music.163.com/#/song?id=22006167", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.id("g_iframe"), "<h1>好听,不錯哦</h1>", "评论成功" },
{ "输入的内容是JS代码", "https://music.163.com/#/song?id=22006168", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.id("g_iframe"), "<script>alert('好听,不錯哦')</script>", "评论成功" },
{ "输入的内容是合法的", "https://music.163.com/#/song?id=22006169", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.id("g_iframe"), "好听,不錯哦", "评论成功" },
{ "输入的内容包含换行符", "https://music.163.com/#/song?id=22006170", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.id("g_iframe"), "好听,不錯哦!\n" + "好听,不錯哦!\n", "评论成功" },
{ "输入的内容包含表情", "https://music.163.com/#/song?id=22006171", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.id("//g_iframe"), "[强][强][强]\n" + "好听,不錯哦!\n", "评论成功" },
{ "输入的内容包含@{账号}", "https://music.163.com/#/song?id=22006172", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.id("g_iframe"), "@云音乐小秘书 厉害了\n" + "好听,不錯哦!\n", "评论成功" }
};
return arr;
}
@DataProvider(parallel=false)
public Object[][] data3(){
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 300; i++) {
sb.append("1");
}
Object[][] arr = {
{ "输入的内容是空,发表评论失败", "https://music.163.com/#/song?id=2697941", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.xpath("//iframe[1]"), "", "输入点内容再提交" },
{ "输入的内容是空格,发表评论失败", "https://music.163.com/#/song?id=2697942", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.xpath("//iframe[1]"), " ", "输入点内容再提交" },
{ "输入的内容过长,发表评论失败", "https://music.163.com/#/song?id=2697943", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.xpath("//iframe[1]"), sb.toString(), "输入不能超过140个字符" },
{ "输入的内容包含特殊字符", "https://music.163.com/#/song?id=2697944", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.xpath("//iframe[1]"), "@好听,不錯哦!@#¥%&*<>\n';,{}[]=+" + "好听,不錯哦!\n", "评论成功" },
{ "输入的内容包含qq号码,发表评论失败", "https://music.163.com/#/song?id=2697945", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.xpath("//iframe[1]"), "发票,QQ876295854", "评论成功" },
{ "输入的内容包含手机号码,发表评论失败", "https://music.163.com/#/song?id=2697946", By.className("area"), By.className("u-btn-1"), By.className("sysmsg"), By.xpath("//iframe[1]"), "发票,17721038951", "评论成功" }
};
return arr;
}
@Test(dataProvider = "data2",enabled = true)
public void f2(String desc, String url, By by6, By by7, By by8, By by10, String content, String expected) throws Exception {
driver.get(url);
Thread.sleep(2000);
driver.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(by10));
List<WebElement> elements = driver.findElements(By.className("u-hd4"));
int index = elements.size()-1;
String comments_count = elements.get(index).getText();//最新评论数
driver.findElement(By.className("u-btni-cmmt")).sendKeys(Keys.ENTER);// 点击评论选项
Thread.sleep(2000);
driver.findElement(by6).sendKeys(content);// 输入评论的内容
driver.findElement(by7).sendKeys(Keys.ENTER);// 点击发布评论
System.out.println(driver.findElement(by8).getText());
assertTrue(driver.findElement(by8).getText().contains(expected), desc);
Thread.sleep(2000);
JavascriptExecutor exec = (JavascriptExecutor)driver;
for(int i=0;i<2;i++){
exec.executeScript("scrollBy(0, 600)");
Thread.sleep(1000);
}
driver.navigate().refresh();
Thread.sleep(3000);
driver.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(by10));
elements = driver.findElements(By.className("u-hd4"));
index = elements.size()-1;
String comments_count2 = elements.get(index).getText();//最新评论数
System.out.println(comments_count2);
assertFalse(comments_count.equals(comments_count2));//验证最新评论数不同
}
@Test(dataProvider = "data3",enabled = false)
public void f3(String desc, String url, By by6, By by7, By by8, By by10, String content, String expected) throws Exception {
driver.get(url);
Thread.sleep(2000);
driver.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(by10));
List<WebElement> elements = driver.findElements(By.className("u-hd4"));
int index = elements.size()-1;
String comments_count = elements.get(index).getText();//最新评论数
System.out.println(comments_count);
driver.findElement(By.className("u-btni-cmmt")).sendKeys(Keys.ENTER);// 点击评论选项
Thread.sleep(2000);
driver.findElement(by6).sendKeys(content);// 输入评论的内容
driver.findElement(by7).sendKeys(Keys.ENTER);// 点击发布评论
System.out.println(driver.findElement(by8).getText());
assertTrue(driver.findElement(by8).getText().contains(expected), desc);
Thread.sleep(2000);
driver.navigate().refresh();
//String html = driver.getPageSource();
//System.out.println(html);
Thread.sleep(3000);
driver.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(by10));
elements = driver.findElements(By.className("u-hd4"));
index = elements.size()-1;
String comments_count2 = elements.get(index).getText();//最新评论数
System.out.println(comments_count2);
assertEquals(comments_count,comments_count2);//验证最新评论数相同
}
@BeforeTest
public void beforeTest() throws Exception {
//设置IE浏览器驱动的路径
System.setProperty("webdriver.ie.driver", "d:\\drivers\\IEDriverServer.exe");
//InternetExplorerOptions option = new InternetExplorerOptions();
//option.requireWindowFocus();
//driver = new InternetExplorerDriver(option);
driver = new InternetExplorerDriver();
// System.setProperty("webdriver.gecko.driver", "d:\\drivers\\geckodriver.exe");
// driver = new FirefoxDriver();
// 设置默认的等待时长
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// 最大化浏览器窗口
//driver.manage().window().maximize();
//
driver.get("https://music.163.com/#/song?id=29099236");
Thread.sleep(2000);
Cookie cookie1 = new Cookie("__csrf","34ed4ebefedea32864da98eee22bd58e","/",null);
Cookie cookie2 = new Cookie("_iuqxldmzr_","32","/",null);
Cookie cookie3 = new Cookie("_ntes_nnid","38aa2dc919e012a7d362c821a50bd058,1557561654225","/",null);
Cookie cookie4 = new Cookie("_ntes_nuid","38aa2dc919e012a7d362c821a50bd058","/",null);
Cookie cookie5 = new Cookie("JSESSIONID-WYYY","RH4ks5ZW2bssEmGOra%2FOi9RSPP1tqu%2B0PVXdW7844odqRqhhWOq86%2BPjM%5C9B%2B4%2FaGkabwY1WSkS9qc%2BPaX%2Bzcuz4OvbdiaksPvVa%5CmyQ%5Cc6gZbU6DbTQ%5CzlpKszPNs2Os8wJTKY%2Bt%5C4d5qMWYGSynTVIOK%2FOTIKuVbqu9FpUhpASuyTO%3A1557563454191","/",null);
Cookie cookie6 = new Cookie("MUSIC_U","37be2a4b6d5e9980fb1f2dbd1a6ec9291310cca37383d14a45a4abb344fa3c56218ec3f8668d838e40044f069169144741049cea1c6bb9b6","/",null);
Cookie cookie7 = new Cookie("WM_TID","Eg7biYM%2BMTpAFEQFVQIsmMg36YXVSOwp","/",null);
//Cookie cookie8 = new Cookie("WM_NI","9mKBslD3uMhXh39HIyhUbLjHnnQheFOBtrZr3GMdU%2FjlaH5KncNzqnZCoDHMv1lEw5cmBUTkhyZlloiRJYBO74uSTcooi8zdluJRd9ur8NshcJlrFYNvKGBjDDJrzuFWQ1A%3D","/",null);
//Cookie cookie9 = new Cookie("WM_NIKE","9ca17ae2e6ffcda170e2e6eeaeb359a2eaac8af772ede78ab6c15b838b9aafb8638ba9a7d8d05d878ca089f72af0fea7c3b92afbb385a3f26888edbbaeeb54acedf7a9cc73ac8baeade944aeb29f89e84e8d9ebc93d97af1998385d825aab4afa9b841aebd829bf4669a9da1a5cb47b6efa4a6f63fa1b38eb6d761aca7968def65ab8e86bac145f5ecbbb4d769adb1a7d9f970b0ab86b3e733b7aef8aff264a196a493c5538eaa85a8c162889c8693f97f86899fd2c437e2a3","/",null);
driver.manage().addCookie(cookie1);
driver.manage().addCookie(cookie2);
driver.manage().addCookie(cookie3);
driver.manage().addCookie(cookie4);
driver.manage().addCookie(cookie5);
driver.manage().addCookie(cookie6);
driver.manage().addCookie(cookie7);
//driver.manage().addCookie(cookie8);
//driver.manage().addCookie(cookie9);
}
@AfterTest
public void afterTest() {
driver.quit();
}
}
测试方法f1的调试日志
日志如下:
[RemoteTestNG] detected TestNG version 6.14.3
Started InternetExplorerDriver server (32-bit)
3.6.0.0
Listening on port 26967
Only local connections are allowed
五月 11, 2019 5:57:57 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: W3C
PASSED: f1("非登录状态,发表评论失败", "https://music.163.com/#/song?id=29099236", By.className: area, By.className: n-log2, "手机号登录")
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
测试方法f2的调试日志
日志如下:
[RemoteTestNG] detected TestNG version 6.14.3
Started InternetExplorerDriver server (32-bit)
3.6.0.0
Listening on port 37307
Only local connections are allowed
五月 11, 2019 5:48:26 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: W3C
评论成功
最新评论(871)
评论成功
最新评论(6)
评论成功
最新评论(7)
评论成功
最新评论(20)
评论成功
最新评论(5)
PASSED: f2("输入的内容是HTML代码", "https://music.163.com/#/song?id=22006167", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.id: g_iframe, "<h1>好听,不錯哦</h1>", "评论成功")
PASSED: f2("输入的内容是JS代码", "https://music.163.com/#/song?id=22006168", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.id: g_iframe, "<script>alert('好听,不錯哦')</script>", "评论成功")
PASSED: f2("输入的内容是合法的", "https://music.163.com/#/song?id=22006169", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.id: g_iframe, "好听,不錯哦", "评论成功")
PASSED: f2("输入的内容包含换行符", "https://music.163.com/#/song?id=22006170", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.id: g_iframe, "好听,不錯哦!
好听,不錯哦!
", "评论成功")
PASSED: f2("输入的内容包含@{账号}", "https://music.163.com/#/song?id=22006172", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.id: g_iframe, "@云音乐小秘书 厉害了
好听,不錯哦!
", "评论成功")
FAILED: f2("输入的内容包含表情", "https://music.163.com/#/song?id=22006171", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.id: //g_iframe, "[强][强][强]
好听,不錯哦!
", "评论成功")
org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #\/\/g_iframe
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-J11D8C6', ip: '192.168.1.7', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_191'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
===============================================
Default test
Tests run: 6, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 6, Failures: 1, Skips: 0
===============================================
测试方法f3的调试日志
日志如下:
[RemoteTestNG] detected TestNG version 6.14.3
Started InternetExplorerDriver server (32-bit)
3.6.0.0
Listening on port 28111
Only local connections are allowed
五月 11, 2019 5:16:38 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: W3C
最新评论(3)
输入点内容再提交吧
最新评论(3)
最新评论(3)
输入点内容再提交吧
最新评论(3)
最新评论(2)
输入不能超过140个字符
最新评论(2)
最新评论(3)
评论成功
最新评论(3)
最新评论(2)
评论成功
最新评论(2)
最新评论(2)
评论成功
最新评论(2)
PASSED: f3("输入的内容是空,发表评论失败", "https://music.163.com/#/song?id=26979242", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.xpath: //iframe[1], "", "输入点内容再提交")
PASSED: f3("输入的内容是空格,发表评论失败", "https://music.163.com/#/song?id=26979243", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.xpath: //iframe[1], " ", "输入点内容再提交")
PASSED: f3("输入的内容过长,发表评论失败", "https://music.163.com/#/song?id=26979244", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.xpath: //iframe[1], "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", "输入不能超过140个字符")
PASSED: f3("输入的内容包含特殊字符", "https://music.163.com/#/song?id=26979239", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.xpath: //iframe[1], "@好听,不錯哦!@#¥%&*<>
';,{}[]=+好听,不錯哦!
", "评论成功")
PASSED: f3("输入的内容包含qq号码,发表评论失败", "https://music.163.com/#/song?id=26979244", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.xpath: //iframe[1], "发票,QQ876295854", "评论成功")
PASSED: f3("输入的内容包含手机号码,发表评论失败", "https://music.163.com/#/song?id=26979244", By.className: area, By.className: u-btn-1, By.className: sysmsg, By.xpath: //iframe[1], "发票,17721038951", "评论成功")
===============================================
Default test
Tests run: 6, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================
参考资料
【参考】Selenium入门中文教程
https://www.yiibai.com/selenium
【参考】Selenium API文档
https://seleniumhq.github.io/selenium/docs/api/java/index.html?index-all.html
【参考】Selenium官方教程
https://www.seleniumhq.org/docs/03_webdriver.jsp
https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp
【参考】xpath的语法
http://www.w3school.com.cn/xpath/xpath_syntax.asp
【参考】Selenium WebDriver问题--Internet Explorer保护模式设置问题
https://www.cnblogs.com/hushaojun/p/4449643.html
【参考】Selenium 调用IEDriverServer打开IE浏览器
https://www.cnblogs.com/misswjr/p/9453566.html
【参考】Selenium IE webdriver 常见的一些问题
https://www.jianshu.com/p/3ee5587ee364
【参考】python+selenium+IE11登陆页面click失效,submit()没有加密问题
https://blog.csdn.net/qq_16045253/article/details/85767410
【参考】selenium中为什么有些IE浏览器中输入英文和数字特别慢
https://blog.csdn.net/qew110123/article/details/85853374
【参考】解决selenium2在IE11上出错的问题,如Unable to get browser
https://blog.csdn.net/chengly0129/article/details/68482829
【参考】selenium操作cookie
https://www.cnblogs.com/moonpool/p/5676673.html
【参考】selenium鼠标键盘事件
https://www.cnblogs.com/sylvia-liu/p/4224409.html
微信扫一扫关注该公众号【测试开发者部落】
![](https://img.haomeiwen.com/i10027900/233689a645a36a12.png)
点击链接加入群聊【软件测试学习交流群】
https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105
网友评论