最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java+testng+selenium的自動化測試實例

 更新時間:2022年11月15日 14:25:48   作者:瀟灑卻風流  
這篇文章主要介紹了java+testng+selenium的自動化測試實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

前言

這是用testng框架加selenium做的一個UI自動化測試的項目

Java代碼

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(){
? ? ?? ?//結(jié)束驅(qū)動程序進程,關(guān)閉瀏覽器
? ? ? ? driver.quit();
? ? }

? ? @Test(groups = {"login"})
? ? public void login() throws InterruptedException {

? ? ? ? //輸入網(wǎng)址(輸入本地項目的URL,下面為本地項目的登陸界面)
? ? ? ? driver.get("http://192.168.0.188/webapp/session/login");
? ? ? ? driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
? ? ? ? //使用其方法獲取瀏覽器類型,并斷言(如果斷言失敗,不會執(zhí)行下面代碼)
? ? ? ? String browserType = driver.getTitle();
? ? ? ? Assert.assertEquals("Google", browserType);
? ? ? ? //獲取賬號框定位
? ? ? ? WebElement userName = driver.findElement(By.xpath("http://*[@id=\"app\"]/div/div/form/div/div[2]/div/div[1]/div/input"));
? ? ? ? //獲取密碼框定位
? ? ? ? WebElement password = driver.findElement(By.xpath("http://*[@id=\"app\"]/div/div/form/div/div[3]/div/div[1]/div/div/span/input"));
? ? ? ? //獲取驗證碼框定位
? ? ? ? WebElement authCode = driver.findElement(By.xpath("http://*[@id=\"app\"]/div/div/form/div/div[4]/div/div/div[1]/div/input"));
? ? ? ? WebElement loginButton = driver.findElement(By.xpath("http://*[@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("http://*[@id=\"logo\"]/div/div/div[1]/h1"));
? ? ? ? String actual = title.getText();
? ? ? ? Assert.assertEquals(actual, "指數(shù)研發(fā)與管理平臺");
? ? }

? ?@Test(description = "定位百度一下")
? ? public void testBaiDu(){
? ? ? ? //輸入網(wǎng)址
? ? ? ? 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");
? ? }
}

配置文件

要想上面的test能跑起來,還需要再pon.xml文件里面添加以下依賴

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.testng</groupId>
? ? ? ? ? ? <artifactId>testng</artifactId>
? ? ? ? ? ? <version>6.14.3</version>
? ? ? ? ? ? <scope>test</scope>
? ? ? ? </dependency>

這是testng框架的依賴,有了這個依賴testng的注釋才會生效

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.seleniumhq.selenium</groupId>
? ? ? ? ? ? <artifactId>selenium-server</artifactId>
? ? ? ? ? ? <version>3.14.0</version>
? ? ? ? </dependency>

如果你只在本地運行代碼,那么有selenium-java就夠了;但是如果要在遠程調(diào)用,就需要配置該selenium-server依賴

?? ??? ?<dependency>
? ? ? ? ? ? <groupId>org.seleniumhq.selenium</groupId>
? ? ? ? ? ? <artifactId>selenium-chrome-driver</artifactId>
? ? ? ? ? ? <version>2.42.2</version>
? ? ? ? </dependency>

這是想要再界面上操作元素配置的依賴

擴展

testng用例可以直接運行java代碼,也可以配置testng.xml文件進行用例的執(zhí)行

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "testProj">
? ? <test name = "testDemo1">

<!-- ? ? ? ?.XML中指定組內(nèi)的某些方法,include為執(zhí)行,exclude為不執(zhí)行-->
? ? ? ? <groups>
? ? ? ? ? ? <run>
? ? ? ? ? ? ? ? <exclude name="fast"/>
? ? ? ? ? ? ? ? <exclude name="slow"/>
? ? ? ? ? ? ? ? <include name="login"/>
? ? ? ? ? ? </run>
? ? ? ? </groups>

<!-- ? ? ? ?.XML指明測試類,按照類名執(zhí)行-->
? ? ? ? <classes>
? ? ? ? ? ? <class name="com.justin.HelloTestNG"/>
? ? ? ? </classes>

<!-- ? ? ? ?.XML指定包名,執(zhí)行包內(nèi)的所有測試類-->
<!-- ? ? ? ?<packages>-->
<!-- ? ? ? ? ? ?<package name="com.justin"></package>-->
<!-- ? ? ? ?</packages>-->
? ? </test>
? ??
? ? <listeners>
? ? ? ? <listener class-name="org.uncommons.reportng.HTMLReporter"></listener>
? ? ? ? <listener class-name="org.uncommons.reportng.JUnitXMLReporter"></listener>
? ? </listeners>
</suite>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

文安县| 高青县| 乌审旗| 年辖:市辖区| 平定县| 南城县| 肇庆市| 广水市| 平昌县| 周口市| 启东市| 湟中县| 子洲县| 永仁县| 扶沟县| 临江市| 伊通| 仁布县| 平远县| 花莲市| 大方县| 辽源市| 屯昌县| 乌拉特前旗| 称多县| 湘潭市| 平昌县| 东港市| 沾化县| 洛川县| 余江县| 宣恩县| 牟定县| 大厂| 宿迁市| 雷州市| 达日县| 八宿县| 常州市| 瑞金市| 全椒县|