package test.day07.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
public class BackStageLoginPage {
WebDriver driver;
//构造函数,初始化时给构造函数赋值
public BackStageLoginPage(WebDriver driver){
this.driver=driver;
}
//账号
public static By accountBy= By.cssSelector("input[name='admin_name']");//用static修饰后,其他class文件就可以用className.accountBy去调用
// 密码
public By passwordBy=By.cssSelector("input[name='admin_pwd']");// 没用static 修改,其他class文件要调用,必须要通过该类的实例化对象来调用 objectName.accountBy
// 验证码
public By verificationCodeBy=By.cssSelector("input[name='code']");
// 记住账号单选框
public By rememberBoxBy=By.cssSelector("span.admin_login_remember");
// 登录后台按钮
public By loginToTheBackgroundButtonBy=By.cssSelector("button.admin_login_btn.denglu");
//欢迎光临前程贷后台
public By welcomeToLoginBacktageBy=By.xpath("//h1[text()='欢迎光临前程贷后台']");
public void login(String account,String password){
driver.findElement(accountBy).sendKeys(account);
driver.findElement(passwordBy).sendKeys(password);
driver.findElement(verificationCodeBy).sendKeys("hapi");
driver.findElement(rememberBoxBy).click();
driver.findElement(loginToTheBackgroundButtonBy).click();
}
}
上述accountBy passwordBy 分别使用static和不适用static,
package test.day07.testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.*;
import test.day07.pages.BackStageIndexLoginPage;
import test.day07.pages.BackStageLoginPage;
import static java.lang.Thread.sleep;
public class BackStageLoginTest {
WebDriver driver;//实例变量:独立于方法之外的变量,不过没有 static 修饰
@BeforeClass
public void setUp()throws InterruptedException{
// 打卡浏览器 、访问前程贷后台、窗口最大化
driver=openBrowser("chrome");
driver.get("http://8.129.91.152:8765/Admin/Index/login.html");
driver.manage().window().maximize();
sleep(1000);
}
@AfterClass
public void tearDown(){
// driver.close();
}
@Test
public void a_loginBackStageTest_001() throws InterruptedException {
BackStageLoginPage backStageLoginPage=new BackStageLoginPage(driver);
backStageLoginPage.login("lemon7","lemonbest");
Thread.sleep(1000);
String expact="欢迎光临前程贷后台";
String actual=driver.findElement(backStageLoginPage.welcomeToLoginBacktageBy).getText();
Assert.assertEquals(actual,expact);
backStageLoginPage.passwordBy;// 没用static修饰,要通过objectName.变量名方式调用
BackStageLoginPage.accountBy;//用了static修饰,可直接通过className.变量名方式调用
}
public WebDriver openBrowser(String browserName) {
if (browserName.equals("chrome")) {
//驱动??? 驱动浏览器(可执行的文件)
//让代码知道chromeDriver驱动是保存在哪里
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
//1、打开浏览器(chrome) 实例化ChromeDriver对象即可
ChromeDriver chromeDriver = new ChromeDriver();
return chromeDriver;
} else if (browserName.equals("firefox")) {
System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver.exe");
FirefoxDriver firefoxDriver = new FirefoxDriver();
return firefoxDriver;
} else if (browserName.equals("ie")) {
//取消IE安全设置(忽略IE的Protected Mode的设置)
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
//忽略浏览器缩放设置的
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe");
//InternetExplorerDriver ieDriver = new InternetExplorerDriver();
InternetExplorerDriver ieDriver = new InternetExplorerDriver(capabilities);
return ieDriver;
}
return null;
}
}
上述@Test代码段内,分别展示了2种不同方式的调用