CourseList

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");
	}
	
	

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值