package com.justin; /** * @author justin-zhu * <p> * 2022年02月23日 16:48 */ 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.*; import java.util.concurrent.TimeUnit; public class HelloTestNG { private WebDriver driver; @BeforeMethod public void setBefore(){ System.setProperty( "webdriver.chrome.driver" , "C:\\Users\\betalpha-qa\\code\\testcode\\TestNG-Demo\\src\\main\\resources\\chromedriver.exe" ); //打开浏览器,使其最大化,并隐性等待两秒钟 driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait( 2 , TimeUnit.SECONDS); } @AfterMethod public void setAfter(){ //结束驱动程序进程,关闭浏览器 driver.quit(); } @Test (groups = { "login" }) public void login() throws InterruptedException { //输入网址(输入本地项目的URL,下面为本地项目的登陆界面) driver.get( "http://192.168.0.188/webapp/session/login" ); driver.manage().timeouts().implicitlyWait( 5 , TimeUnit.SECONDS); //使用其方法获取浏览器类型,并断言(如果断言失败,不会执行下面代码) String browserType = driver.getTitle(); Assert.assertEquals( "Google" , browserType); //获取账号框定位 WebElement userName = driver.findElement(By.xpath( "//*[@id=\"app\"]/div/div/form/div/div[2]/div/div[1]/div/input" )); //获取密码框定位 WebElement password = driver.findElement(By.xpath( "//*[@id=\"app\"]/div/div/form/div/div[3]/div/div[1]/div/div/span/input" )); //获取验证码框定位 WebElement authCode = driver.findElement(By.xpath( "//*[@id=\"app\"]/div/div/form/div/div[4]/div/div/div[1]/div/input" )); WebElement loginButton = driver.findElement(By.xpath( "//*[@id=\"app\"]/div/div/form/div/div[5]/div/div/div/button" )); //输入账号密码登录,并点击登录 userName.sendKeys( "jusitn@qq.com" ); password.sendKeys( "123456" ); authCode.sendKeys( "1234" ); driver.manage().timeouts().implicitlyWait( 5 ,TimeUnit.SECONDS); loginButton.click(); driver.manage().timeouts().implicitlyWait( 30 ,TimeUnit.SECONDS); //获取登录界面的title,验证登录成功 WebElement title = driver.findElement(By.xpath( "//*[@id=\"logo\"]/div/div/div[1]/h1" )); String actual = title.getText(); Assert.assertEquals(actual, "指数研发与管理平台" ); } @Test (description = "定位百度一下" ) public void testBaiDu(){ //输入网址 driver.get( "https://www.baidu.com/" ); driver.manage().timeouts().implicitlyWait( 2 ,TimeUnit.SECONDS); //定位到百度一下按钮 WebElement name = driver.findElement(By.id( "su" )); String text = name.getAttribute( "value" ); Assert.assertEquals(text, "百度一下" ); } @Test (groups = { "fast" }) public void aFastTest(){ System.out.println( "Fast test" ); } @Test (groups = { "slow" }) public void aSlowTest(){ System.out.println( "Slow test" ); } } |