package com.mushishi.Imooc;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class CourseList {
public WebDriver driver;
@BeforeClass
public void beforeClass() {
System.setProperty("webdriver.chrome.driver", "D:\\java\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://coding.imooc.com");
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.className("js-close")).click();
}
@AfterClass
public void afterClass() {
driver.close();
}
/*
@Test
public void test01() {
List<String> listString = ListCourseTitle();
for(int i=0;i<listString.size();i++) {
//String courseTitle = courseName.getText();
//courseName.click();
//Node.js 从零开发 web server博客项目 前端晋升全栈工程师必备
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p[contains(@title,'前端晋升全栈工程师必备')]
driver.findElement(By.xpath("//p[@title='"+listString.get(i)+"']")).click();
driver.navigate().back();
driver.findElement(By.className("js-close")).click();
System.out.println(listString.get(i));
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Assert.assertEquals(1, 2);
System.out.println("第一个case");
}
*/
@Test
public void test02() {
List<Integer> numList = pageNumList();
for (int j=0;j<numList.size()-1;j++) {
List<WebElement> courseList = driver.findElements(By.className("shizan-name"));
for(int i=0;i<courseList.size();i++) {
courseList.get(i).click();
driver.navigate().back();
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.className("js-close")).click();
courseList = driver.findElements(By.className("shizan-name"));
}
driver.findElement(By.linkText("下一页")).click();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
@Test
public void test03() {
List<WebElement> courseList = driver.findElements(By.className("shizan-name"));
List<Integer> numList = pageNumList();
for (int i=0;i<numList.size()-1;i++) {
for(WebElement courseName:courseList) {
System.out.println(courseName.getText());
courseName.click();
driver.navigate().back();
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.className("js-close")).click();
courseList = driver.findElements(By.className("shizan-name"));
}
driver.findElement(By.partialLinkText("下一页"));
}
}
**/
public List<Integer> pageNumList() {
List<Integer> pageNumList = new ArrayList<Integer>();
List<WebElement> aElementList = driver.findElement(By.className("page")).findElements(By.tagName("a"));
for(WebElement aElement:aElementList) {
String pageNum = aElement.getText();
if (isNumber(pageNum) == true){
pageNumList.add(Integer.valueOf(pageNum).intValue());
}
}
return pageNumList;
}
public boolean isNumber(String pageNum) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(pageNum);
if(isNum.matches()) {
return true;
}
return false;
}
public List<String> ListCourseTitle(){
List<String> listCourseTitle = new ArrayList<String>();
List<WebElement> courseList = driver.findElements(By.className("shizan-name"));
for(WebElement courseName : courseList) {
String courseTitle = courseName.getText();
listCourseTitle.add(courseTitle);
}
return listCourseTitle;
}
@BeforeMethod
public void beforeMethod() {
System.out.println("第一个case before");
}
@AfterMethod
public void afterMethod() {
System.out.println("第一个case after");
}
}
CourseList
最新推荐文章于 2022-05-13 10:58:16 发布