selenium

package com.my.homework;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;

public class BussinessLib {


//登录基础版方法
public void login(WebDriver driver,String p_user,String p_pwd){


driver.findElement(parseObject(ObjectStore.Login_LoginLink)).click();
driver.findElement(parseObject(ObjectStore.Login_Name)).clear();
driver.findElement(parseObject(ObjectStore.Login_Name)).sendKeys(p_user);
driver.findElement(parseObject(ObjectStore.Login_Password)).clear();
driver.findElement(parseObject(ObjectStore.Login_Password)).sendKeys(p_pwd);
driver.findElement(parseObject(ObjectStore.Login_Button)).click();

driver.findElement(parseObject(ObjectStore.Login_LoginLink)).click();
// driver.findElement(parseObject(ObjectStore.Login_Name)).sendKeys("");
// driver.findElement(parseObject(ObjectStore.Login_Name)).isEnabled();
//driver.findElement(parseObject(ObjectStore.Data_1)).click();


try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//判断是否有广告出现
if (isElementPresent(driver,By.xpath("//*[@id='ng-app']/body/div[1]/div/div/div/div[1]/span/i"))) //xpath元素是广告右上角的*
driver.findElement(By.xpath("//*[@id='ng-app']/body/div[1]/div/div/div/div[1]/span/i")).click();


}
//可以多一个参数,也可以把项目中的任务、日历、文件、话题... 作为参数提取,但是代码会很冗长,所以不建议
public void createfile(WebDriver driver,String p_path,String p_project,String p_folder) throws Exception{
driver.findElement(By.linkText("项目")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//span[contains(text(),""+p_project+"")]")).click();
Thread.sleep(3000);
driver.findElement(By.linkText("文件")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//a[contains(text(),""+ p_folder+"")]")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//button[contains(text(), '添加文件')]")).click();
Thread.sleep(3000);
RobotKeyboard.getInstance().type(p_path); //输入要上传的文件
Thread.sleep(1000);
RobotKeyboard.getInstance().typeKey("Tab", 2); //点击2次tab按钮 ,焦点到windows窗口“打开”
Thread.sleep(1000);
RobotKeyboard.getInstance().typeKey("Enter", 1); //点击 回车按钮,上传文件
Thread.sleep(2000);


}

public By parseObject(String p_object) {
String newObjecyt = null;

if (p_object.startsWith(".//") || p_object.startsWith("//")) {
return By.xpath(p_object);
} else if (p_object.startsWith("link=") || p_object.startsWith("Link=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.linkText(newObjecyt);
} else if (p_object.startsWith("xpath=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.xpath(newObjecyt);
} else if (p_object.startsWith("id=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.id(newObjecyt);
} else if (p_object.startsWith("css=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.cssSelector(newObjecyt);
} else if (p_object.startsWith("class=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.className(newObjecyt);
} else if (p_object.startsWith("tagName=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.tagName(newObjecyt);
} else if (p_object.startsWith("name=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.name(newObjecyt);
} else
return null;

}

public boolean isElementPresent(WebDriver driver,By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}

public void logout(WebDriver driver){
driver.findElement(By.cssSelector("span.avatar-text")).click();
driver.findElement(By.linkText("退出登录")).click();
System.out.println("***************退出登录***************");
}

}

-------------------------------------

package com.my.homework;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;


public class CreateFileCaseTest5 {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
BussinessLib bl;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@Before
public void setUp() throws Exception {
bl=new BussinessLib();
driver = new FirefoxDriver();
baseUrl = "https://my.worktile.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get(baseUrl + "/");
driver.manage().window().maximize();
Thread.sleep(5000);
//登录基础版
bl.login(driver,"ml0tester","123456");
//验证登陆是否成功
assertTrue(bl.isElementPresent(driver,By.xpath("//*[@id='btn_leftmenu_shortcut_create']/i")));
Thread.sleep(2000);
System.out.println("***************确认登录成功***************");
}

@Test
public void testCreateFileCase() throws Exception {


bl.createfile(driver,"D:\ml08tester.txt","ML0八期","练习");
assertTrue(bl.isElementPresent(driver,By.xpath("//a[contains(text(), 'ml08tester.txt')]")));
System.out.println("***************确认上传成功***************");




}

@After
public void tearDown() throws Exception {
bl.logout(driver);
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}


}

-------------------------

bojectstore

package com.my.homework;

public class ObjectStore {
//引入parseObject 之前
// public static final String Login_Name="login_name";
// public static final String Login_Password="login_password";
// public static final String Login_Button="//button[@type='button']";
// public static final String Data_1="id=leftmenu_backdrop";
// public static final String Data_2="link=ML0八期";

// 登陆相关 UI
public static final String Login_LoginLink="//*[contains(text(), '登录基础版')]";
public static final String Login_Name="name=login_name";
public static final String Login_Password="name=login_password";
public static final String Login_Button="//button[@type='button']";


//创建任务相关UI

}

----------------------------------------

bussinessLib

package com.my.homework;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;

public class BussinessLib {


//登录基础版方法
public void login(WebDriver driver,String p_user,String p_pwd){


driver.findElement(parseObject(ObjectStore.Login_LoginLink)).click();
driver.findElement(parseObject(ObjectStore.Login_Name)).clear();
driver.findElement(parseObject(ObjectStore.Login_Name)).sendKeys(p_user);
driver.findElement(parseObject(ObjectStore.Login_Password)).clear();
driver.findElement(parseObject(ObjectStore.Login_Password)).sendKeys(p_pwd);
driver.findElement(parseObject(ObjectStore.Login_Button)).click();

driver.findElement(parseObject(ObjectStore.Login_LoginLink)).click();
// driver.findElement(parseObject(ObjectStore.Login_Name)).sendKeys("");
// driver.findElement(parseObject(ObjectStore.Login_Name)).isEnabled();
//driver.findElement(parseObject(ObjectStore.Data_1)).click();


try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//判断是否有广告出现
if (isElementPresent(driver,By.xpath("//*[@id='ng-app']/body/div[1]/div/div/div/div[1]/span/i"))) //xpath元素是广告右上角的*
driver.findElement(By.xpath("//*[@id='ng-app']/body/div[1]/div/div/div/div[1]/span/i")).click();


}
//可以多一个参数,也可以把项目中的任务、日历、文件、话题... 作为参数提取,但是代码会很冗长,所以不建议
public void createfile(WebDriver driver,String p_path,String p_project,String p_folder) throws Exception{
driver.findElement(By.linkText("项目")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//span[contains(text(),""+p_project+"")]")).click();
Thread.sleep(3000);
driver.findElement(By.linkText("文件")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//a[contains(text(),""+ p_folder+"")]")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//button[contains(text(), '添加文件')]")).click();
Thread.sleep(3000);
RobotKeyboard.getInstance().type(p_path); //输入要上传的文件
Thread.sleep(1000);
RobotKeyboard.getInstance().typeKey("Tab", 2); //点击2次tab按钮 ,焦点到windows窗口“打开”
Thread.sleep(1000);
RobotKeyboard.getInstance().typeKey("Enter", 1); //点击 回车按钮,上传文件
Thread.sleep(2000);


}

public By parseObject(String p_object) {
String newObjecyt = null;

if (p_object.startsWith(".//") || p_object.startsWith("//")) {
return By.xpath(p_object);
} else if (p_object.startsWith("link=") || p_object.startsWith("Link=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.linkText(newObjecyt);
} else if (p_object.startsWith("xpath=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.xpath(newObjecyt);
} else if (p_object.startsWith("id=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.id(newObjecyt);
} else if (p_object.startsWith("css=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.cssSelector(newObjecyt);
} else if (p_object.startsWith("class=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.className(newObjecyt);
} else if (p_object.startsWith("tagName=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.tagName(newObjecyt);
} else if (p_object.startsWith("name=")) {
newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
return By.name(newObjecyt);
} else
return null;

}

public boolean isElementPresent(WebDriver driver,By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}

public void logout(WebDriver driver){
driver.findElement(By.cssSelector("span.avatar-text")).click();
driver.findElement(By.linkText("退出登录")).click();
System.out.println("***************退出登录***************");
}

}

原文地址:https://www.cnblogs.com/wjy123/p/7659197.html