5、编写自动化脚本

前面准备工作已完成,可以开始写脚本了。

重点见红框

1、在类前面添加监听Listener,当然也可以在testng.xml中设置,后面再说

2、webtestListener.driver = driver;这句将当前的driver传递给webtestListener的driver,才能截图成功,见后续代码


下面将依次贴出脚本代码:

1、test_baidu_news.java代码:

package testSuite;


import org.testng.annotations.Test;


import framework.BrowserEngine;
import framework.webtestListener;
import pageobjects.baidu_home_page;
import pageobjects.news_page;


import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;




@Listeners({webtestListener.class})
public class test_baidu_news {

  
  WebDriver driver;
  
  @Test
  public void test_news_zhaolei() {
 
 baidu_home_page bhp = new baidu_home_page(driver);
 bhp.click_news();
 news_page np = new news_page(driver);
 np.send_text("赵雷");
 np.click_submit_news();
 np.sleep(5);
 
  }
  
  
  @BeforeClass
  public void beforeClass(){
 BrowserEngine be = new BrowserEngine();
 driver = be.getBrowser();
 webtestListener.driver = driver;
  }


  @AfterClass
  public void afterClass() {
 driver.quit();
  }


}


2、test_baidu_tieba.java代码:

package testSuite;


import org.testng.annotations.Test;


import framework.BrowserEngine;
import framework.webtestListener;
import pageobjects.baidu_home_page;
import pageobjects.news_page;
import pageobjects.tieba_page;


import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;




@Listeners({webtestListener.class})
public class test_baidu_tieba {

  
  WebDriver driver;
  
  @Test
  public void test_news_zhoujielun() {
 
 baidu_home_page bhp = new baidu_home_page(driver);
 bhp.tieba().click();
 tieba_page tp = new tieba_page(driver);
 tp.tieba_input_ele().sendKeys("周杰伦");
 tp.tieba_submit_ele().click();
 tp.sleep(5);
 
  }
  
  @BeforeClass
  public void beforeClass(){
 BrowserEngine be = new BrowserEngine();
 driver = be.getBrowser();
 webtestListener.driver = driver;
  }


  @AfterClass
  public void afterClass() {
 driver.quit();
  }


}


3、test_baidu_vedio.java代码:

package testSuite;


import org.testng.annotations.Test;


import framework.BrowserEngine;
import framework.webtestListener;
import pageobjects.baidu_home_page;
import pageobjects.vedio_page;


import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;


@Listeners({webtestListener.class})
public class test_baidu_vedio {

  WebDriver driver;
  
  @Test
  public void test_baidu_vedio_mayun() {
 baidu_home_page bhp = new baidu_home_page(driver);
 bhp.click_vedio();
 vedio_page vp = new vedio_page(driver);
 vp.input_vedio_element().sendKeys("马云");
 vp.submit_vedio_element().click();
 vp.sleep(5);
  }
  
  @BeforeClass
  public void beforeClass() {
 BrowserEngine be = new BrowserEngine();
 driver = be.getBrowser();
 webtestListener.driver = driver;
  }


  @AfterClass
  public void afterClass() {
 driver.quit();
  }


}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页