这前后章节不连贯呀,照着敲懵了,现在运行报一个空指针错误,debug的时候login的driver为null

来源:8-13 添加购物车case

dong辉

2019-11-12

图片描述图片描述图片描述

1-login.class
package com.mushishi.imooc.runcase;

import com.mushishi.imooc.business.CoursePagePro;
import com.mushishi.imooc.handle.CoursePageHandle;
import com.mushishi.imooc.page.BasePage;
import com.mushishi.imooc.page.CoursePage;
import com.mushishi.imooc.page.ProUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Login {
public WebDriver driver;
public CoursePagePro cpp;
public CoursePageHandle c1;
public Login(){
//this.driver =driver1;
cpp = new CoursePagePro(driver);
c1 = new CoursePageHandle(driver);
}
@BeforeClass
public void courseList1() {
// 加载配置文件
System.setProperty(“webdriver.chrome.driver”, “E:seleniumchromedriver.exe”);
// 实例化driver对象
driver = new ChromeDriver();
driver.get(“https://www.imooc.com/”);
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElement(By.className(“redrain-closeBtn”)).click();
}
@Test
public void checkBox() {
// ID定位
driver.findElement(By.id(“js-signin-btn”)).click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// name定位
driver.findElement(By.name(“email”)).sendKeys(“用户名”);
// className定位
driver.findElement(By.className(“js-loginPassword”)).sendKeys(“密码”);
//登录按钮
driver.findElement(By.className(“moco-btn-red”)).click();
//等待页面加载
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.get(“https://coding.imooc.com/class/330.html”);
}
/**
* 添加购物车
*/
@Test(dependsOnMethods = {“checkBox”})
public void TestAddCart(){
//driver.findElement(By.id(“buy-trigger”)).click();
cpp.addCart();
// c1.clickBuyNow();
//c1.clickaddCart();
}

}

2-coursePage.class
package com.mushishi.imooc.business;

import com.mushishi.imooc.handle.CoursePageHandle;
import org.openqa.selenium.WebDriver;

public class CoursePagePro {
public WebDriver driver;
//初始化coursePageHandle对象
public CoursePageHandle coursePageHandle;
//构造方法
public CoursePagePro(WebDriver driver){
//this.driver = driver;
coursePageHandle = new CoursePageHandle(driver);
}
/**
* 添加购物车
*/
public void addCart(){
//定义一个整型存放首次获取的int值
int beforNum;
//定义一个String,存放第二次的string值
String afterCourseNum;
String courseNum=coursePageHandle.getshopcartNum();
//try-cath一下
try{
//强转成int类型
beforNum = Integer.valueOf(courseNum);
}catch(Exception e){
beforNum =0;
}
//如果brforNum为0时,添加购物车
coursePageHandle.clickaddCart();
//再次获取存入变量
afterCourseNum = coursePageHandle.getshopcartNum();
//
int afterNum = Integer.valueOf(afterCourseNum);
//做判断,如果afternum=befornum+1就证明添加购物车成功
if(afterNum == beforNum+1){
System.out.println(“添加购物车成功!”);
coursePageHandle.clickshopcart();
}
}

}

3-CoursePageHandle.class
package com.mushishi.imooc.handle;

import com.mushishi.imooc.page.CoursePage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class CoursePageHandle {
public WebDriver driver;
//初始化coursePage对象
public CoursePage coursePage;

//构造方法
public CoursePageHandle(WebDriver driver) {
    this.driver = driver;
    coursePage = new CoursePage(driver);
}

/**
 * 点击立即购买按钮
 */
public void clickBuyNow() {
    coursePage.click(coursePage.getBuytriggerElement());
}

/**
 * 点击添加购物车
 */
public void clickaddCart() {
    coursePage.click(coursePage.getAddCartElement());
}

/**
 * 点击右上角购物车
 */
public void clickshopcart() {
    coursePage.click(coursePage.getShopCartElement());
}
/**
 * 获取购物车数量
 */
public String getshopcartNum(){
    WebElement element= coursePage.getShopCartNumElement();
    return coursePage.gettext(element);
}
/**
 * 获取课程名称
 */
public String getCourseName(){
    WebElement element = coursePage.getCouserNameElement();
    return  coursePage.gettext(element);
}

}

4–CoursePage.class
package com.mushishi.imooc.page;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

/**

  • @author libw-c
    */
    public class CoursePage extends BasePage{

    public CoursePage(WebDriver driver) {
    super(driver);
    }
    /**

    • 获取立即购买按钮
      /
      public WebElement getBuytriggerElement(){
      return GetElement(“BuyNow”);
      }
      /
      *
    • 获取添加购物车element
      /
      public WebElement getAddCartElement(){
      return GetElement(“addCart”);
      }
      /
      *
    • 获取右上角购物车element
      /
      public WebElement getShopCartElement(){
      return GetElement(“shopcart”);
      }
      /
      *
    • 获取购物车数量
      /
      public WebElement getShopCartNumElement(){
      return GetElement(“shopcartNum”);
      }
      /
      *
    • 获取课程详情页面左上角课程名element
      */
      public WebElement getCouserNameElement(){
      return nodeElement(“courseInfo”,“courseInfoText”);
      }
      }

5–BasePage.class
package com.mushishi.imooc.page;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public class BasePage {
//实例化log4j
static Logger logger=Logger.getLogger(BasePage.class);
//声明driver
public WebDriver driver;
//构造方法,传入driver
public BasePage(WebDriver driver){
//因为driver1是没有数值,需要给它进行赋值
this.driver=driver;
//driver1 = new CoursePage(driver);
}

/**
 * Element方法封装
 */
public WebElement GetElement(String key){
    boolean flag=true;
    int i =0;
    WebElement Element=null;
    while(flag) {
        try {
            Element = driver.findElement(this.GetByLocal(key));
            flag=false;

        }
        catch(Exception e) {
            i=i+1;
            if(i ==10) {
                flag=false;
            }
        }
    }
    return Element;
}
/**
 * 层级定位,通过父节点定位到子节点
 * 需要传入父节点和子节点by
 */
public WebElement nodeElement(String key,String nodekey){
    WebElement element = driver.findElement(this.GetByLocal(key));
    return element.findElement(this.GetByLocal(nodekey));
}

/**
 * 层级定位,通过父节点定位到子节点
 * 需要传入父节点和子节点by
 */

// public WebElement nodeElement(By by,By nodeby){
// WebElement el = this.element(by);
// return el.findElement(nodeby);
// }
/**
* 封装一个click点击
/
public void click(WebElement element){
//判断传入的是否存在
if(element != null){
element.click();
}else{
System.out.println(“元素没有定位到,点击失败!”+element);
}
}
/
*
* 获取文本信息
*/
public String gettext(WebElement element){
return element.getText();
}

/**
 * 元素定位类型封装
 */
public By GetByLocal(String key) {
    ProUtil pro = new ProUtil("element1.properties");
    //定位信息输出debug(key)值
    logger.debug("你的定位信息的key为:"+key);
    //key比对,
    String Locator = pro.GetPro(key);
    // value值进行拆分
    String LocatorBy = Locator.split("<")[0];
    String LocatorValue = Locator.split("<")[1];
    logger.debug("你的定位方式为"+LocatorBy);
    logger.debug("你的定位值为"+LocatorValue);
    // 判断对应BY类型
    if (LocatorBy.equals("id")) {
        return By.id(LocatorValue);
    } else if (LocatorBy.equals("name")) {
        return By.name(LocatorValue);
    } else if (LocatorBy.equals("className")) {
        return By.className(LocatorValue);
    } else {
        return By.xpath(LocatorValue);
    }
}

/**
 * 鼠标悬停
 * @param
 */
public void MoveToElement(WebElement ToElement){
    //new action对象
    Actions actions = new Actions(driver);
    //鼠标悬停在图片Elemet上面
    actions.moveToElement(ToElement).perform();
}

}

报错截图:
图片描述
图片描述
图片描述

写回答

2回答

Mushishi

2019-11-12

你好,整个课程后面重构了,学到po之后这些是之前课程 累计没有删除的,课程更新重构了,视频源和思路变更了,后面的作为拓展,如果有问题也可以反馈

0
0

Mushishi

2019-11-12

login.java
的68你是不是driver没传递进去

0
2
Mushishi
回复
dong辉
每一个方法开始的地方进行打印
2019-11-23
共2条回复

Web自动化测试 Selenium基础到企业应用

零基础学习Web自动化,测试面试必问技术点解答,高薪在向你招手

1237 学习 · 862 问题

查看课程