day23java學(xué)習(xí).png
package com.guoyasoft.autoUI.guoya_1810;
//引入 java代碼路徑
import com.guoyasoft.autoUI.common.BaseUI;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;
public class GuoyaLogin extends BaseUI {
//public 公開的方法 void 無返回 login() 方法名
//實(shí)例變量/全局變量 或者用private私有的
public String username="guoya295";
public String password="qweasd";
public String realname="黃哈哈";
public String phone="18916968152";
public String age="25";
public String users []={
"ye008",
"ye007",
"ye006",
"ye005",
"ye004",
"ye003",
"ye002",
"ye001",
"ye000"};
//添加testng 注解用來執(zhí)行測試方法
@Test
public void login(){
//設(shè)置循壞 起始值,最大值/最小值 增量,減量
// for(int i=0; i<users.length; i++ ) {i
System.out.println("當(dāng)前循環(huán)次數(shù)" );
sleep(1000);
//打開登錄網(wǎng)頁
//driver.get("http://47.98.226.232:8080/guoya-medium/jsp/user/login.jsp");
url("http://47.98.226.232:8080/guoya-medium/jsp/user/login.jsp");
//線程休眠
sleep(1000);
//查找元素根據(jù)name查找 然后執(zhí)行清除
driver.findElement(By.name("userName")).clear();
//查找元素根據(jù)name查找 執(zhí)行輸入
driver.findElement(By.name("userName")).sendKeys(users[0]);
//查找元素根據(jù)id查找 然后執(zhí)行清除
driver.findElement(By.id("password")).clear();
//查找元素根據(jù)id查找 執(zhí)行輸入
driver.findElement(By.id("password")).sendKeys(password);
//查找元素根據(jù)xpath查找驗(yàn)證碼 執(zhí)行輸入
//driver.findElement(By.xpath("http://input[@name='checkCode']")).sendKeys("12345");
send("http://input[@name='checkCode']","12345");
//查找元素根據(jù)xpath查找 點(diǎn)擊登錄
click("http://input[@id='loginBtn']");
//queryalluser();
//調(diào)用自定義方法
//queryuser();
//queryage();
//boolean 布爾類型 true真 false假
boolean guoya =driver.getPageSource().contains("學(xué)生查詢");
//assert斷言 判斷預(yù)期結(jié)果與實(shí)際結(jié)果是否相等
Assert.assertEquals(guoya,true,"用戶登錄頁面失敗");
queryuser(users[0]);
//切換iframe窗口至結(jié)果展示窗口
driver.switchTo().frame("result");
//判斷切換結(jié)果展示頁面是否包含查詢用戶
Assert.assertEquals(driver.getPageSource().contains(users[0]),true);
//打印新的頁面代碼
System.out.println(driver.getTitle());
//切換回默認(rèn)窗口
driver.switchTo().defaultContent();
// }
}
@Test
public void signup() {
//最小值,最大值,增量,判斷條件
int i = 0;
//條件成立則一直執(zhí)行循環(huán),條件不滿足結(jié)束
while (i < 10){
//boolean result;
//while (result=true) {
//打開注冊網(wǎng)頁
driver.get("http://47.98.226.232:8080/guoya-medium/jsp/user/signUp.jsp");
sleep(1000);
WebElement element = driver.findElement(By.id("userName"));
element.clear();
element.sendKeys(username);
driver.findElement(By.id("realName")).sendKeys(realname);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.id("password2")).sendKeys(password);
driver.findElement(By.id("phone")).sendKeys(phone);
driver.findElement(By.id("age")).sendKeys(age);
driver.findElement(By.xpath("http://input[@id='checkCode']")).sendKeys("1234");
//點(diǎn)擊注冊
driver.findElement(By.xpath("http://input[@id='submitBtn']")).click();
//彈出彈窗 是否確定
Alert alert = driver.switchTo().alert();
alert.accept();
//alert.dismiss();
//boolean result = driver.getPageSource().contains("登錄界面");
//如果條件為真 打印注冊成功
// if (result == true) {
// System.out.println("用戶注冊成功");
//否則就是注冊失敗
// } else {
// System.out.println("用戶注冊失敗");
// }
// result=driver.getPageSource().contains("登錄界面");
//System.out.println("注冊成功");
//}
i++;
System.out.println("當(dāng)前循環(huán)次數(shù)" + i);
}
}
//全部查詢
public void queryalluser(){
driver.findElement(By.xpath("http://input[@type='submit']")).click();
sleep(3000);
}
public void queryuser(String name){
driver.findElement(By.xpath("http://input[@name='userName']")).sendKeys(name);
driver.findElement(By.xpath("http://input[@type='submit']")).click();
sleep(3000);
}
public void queryage(){
driver.findElement(By.xpath("http://input[@name='userName']")).clear();
driver.findElement(By.xpath("(//input[@type='number'])[1]")).sendKeys(age);
driver.findElement(By.xpath("(//input[@type='submit'])")).click();
sleep(3000);
driver.findElement(By.xpath("http://input[@name='userName']")).clear();
driver.findElement(By.xpath("(//input[@type='number'])[1]")).sendKeys(age);
driver.findElement(By.xpath("(//input[@type='submit'])")).click();
sleep(3000);
}
public void click(String xpath){
driver.findElement(By.xpath(xpath)) .click();
}
public void send(String xpath,String sendkey) {
driver.findElement(By.xpath(xpath)).sendKeys(sendkey);
}
public void url(String url){
driver.get(url);
}
//public void id(String id,String realname){
// driver.findElement(By.id(id)).sendKeys(realname);
//}
}