TestNG+Java+Selenium+Maven 代码实例

搭环境时注意不要忘记testng的jar包,selenium的jar包

package com.guge.test;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;

public class Guge {
WebDriver driver=new FirefoxDriver();

@Test
public void login() throws InterruptedException {
//driver.get("http://guge.user.yunpaas.cn/gugeUser/index.html#/home");
driver.manage().window().maximize();//窗口最大化

driver.get("http://www.baidu.com/");

//账号密码,获取验证码
driver.findElement(By.xpath(".//*[@id='app']/div/div[1]/form/div[1]/div/div[1]/input")).sendKeys("13284079862");
driver.findElement(By.xpath(".//*[@id='app']/div/div[1]/form/div[2]/div/div/input")).sendKeys("123456");
driver.findElement(By.className("getcode")).click();

//控制台输入验证码
Scanner sc = new Scanner(System.in);
System.out.println("请输入6位数字验证码:(按回车键继续)");
String code = sc.nextLine();

driver.findElement(By.xpath(".//*[@id='app']/div/div[1]/form/div[3]/div/div/input")).sendKeys(code); //控制台的验证码填写在输入框里
driver.findElement(By.className("login_from_btn")).click();
System.out.println("登录中...");

//断言
Thread.sleep(8000);
Assert.assertEquals(driver.findElement(By.xpath(".//*[@id='app']/div/div[2]/div/div/div[1]/div[1]/div/span[1]")).getText(),"欢迎 !","登录失败!"); //实际值,预期值,提示信息
System.out.println("登录成功......");
}

@Test
public void denglubtn(){
Assert.assertEquals(driver.findElement(By.xpath(".//*[@id='app']/div/div[2]/div/div/div[1]/div[1]/div/span[2]")).getText(),"北京聚通达科技股份有限公司(勿删)","公司名称错误!");
//driver.findElement(By.xpath(".//*[@id='app']/div/div[1]/div[1]/ul/li[1]/span")).click();
//driver.findElement(By.xpath(".//*[@id='app']/div/div[2]/div/div/div[2]/div/div[1]/div[1]/div[2]")).click();
}

@BeforeMethod
public void beforeMethod() {
}

@AfterMethod
public void afterMethod() {
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}

原文地址:https://www.cnblogs.com/Susie-/p/10413940.html