另一種思路(針對(duì)swfupload上傳插件)
1户侥、使用httpclient上傳附件
2、調(diào)用JS腳本,往表單里面添加跟附件有關(guān)的輸入項(xiàng)
image.png
設(shè)計(jì)上傳圖片的腳本
代碼如下:
/**
* 上傳附件
* @return
* @throws Exception
*/
public String uploadJPG() throws Exception{
//得到一個(gè)客戶(hù)端(用來(lái)發(fā)送請(qǐng)求的)
CloseableHttpClient client = HttpClients.createDefault();
//創(chuàng)建一個(gè)Post請(qǐng)求
HttpPost request = new HttpPost("http://192.168.1.8:8090/phpwind/index.php?c=upload&a=dorun&_json=1&_json=1");
//entity表示主體(body)
//StringEntity entity = null;
HttpEntity entity = null;
//response表示響應(yīng)
HttpResponse response = null;
//headers表示消息頭(response headers)
Header[] headers = null;
String aid = null;
try {
//設(shè)置請(qǐng)求消息主體的內(nèi)容
entity = MultipartEntityBuilder.create().
addTextBody("Filename", "mm02.jpg").
addTextBody("fid", "2").
addTextBody("Bzi_winduser","msL4udGA%2BPNSm7bGPAdLhpDKMwseQABD2Z6iMxwPN%2FrtT65uQOO6cw%3D%3D").
addTextBody("Bzi_visitor","s8hCqCZfxL3LMvGATleiatclmAgxUTQtW9P65vLo1aY%3D").
addTextBody("Bzi_lastvisit","132%091542205707%09%2Fphpwind%2Findex.php%3Fm%3Demotion%26type%3Dbbs%26csrf_token%3D8434d84b23423e26").
addTextBody("Bzi_visit_referer","fid_2_page_1").
addTextBody("Upload","Submit Query").
addTextBody("csrf_token","8434d84b23423e26").
addBinaryBody("Filedata", new File("C:\\Users\\yangzc\\Desktop\\temp05\\images\\mm02.jpg")).build();
request.setHeader("Cookie","csrf_token=8434d84b23423e26; Bzi_visit_referer=fid_2_page_1; Bzi_winduser=msL4udGA%2BPNSm7bGPAdLhpDKMwseQABD2Z6iMxwPN%2FrtT65uQOO6cw%3D%3D; Bzi_visitor=s8hCqCZfxL3LMvGATleiatclmAgxUTQtW9P65vLo1aY%3D; Bzi_lastvisit=132%091542205707%09%2Fphpwind%2Findex.php%3Fm%3Demotion%26type%3Dbbs%26csrf_token%3D8434d84b23423e26");
request.setHeader("X-Requested-With","ShockwaveFlash/31.0.0.148");
//將entity設(shè)置為post請(qǐng)求的主體
request.setEntity(entity);
//發(fā)送該post請(qǐng)求
response = httpClient.execute(request);
//
headers = httpResponse.getAllHeaders();
//輸出響應(yīng)消息頭的內(nèi)容
for(Header h:headers){
System.out.println(h.toString());
}
String responseBody = EntityUtils.toString(response.getEntity());
//輸出響應(yīng)消息主體的內(nèi)容
System.out.println(responseBody);
JSONObject obj = (JSONObject)JSON.parse(responseBody);
JSONObject data = (JSONObject)obj.get("data");
aid = (String)data.get("aid");
System.out.println("附件id是:" + aid);
} catch (Exception e) {
e.printStackTrace();
}
client.close();
return aid;
}
往表單里面添加跟附件有關(guān)的輸入項(xiàng)
String script = "$('#mainForm').append(\"<input type='text' name='flashatt["+aid+"][desc]'>\");"
+ "$('#mainForm').append(\"<input type='text' name='flashatt["+aid+"][cost]'>\");"
+ "$('#mainForm').append(\"<input type='text' name='flashatt["+aid+"][ctype]' value='1'>\");";
完整代碼
package webtest;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class JSTest {
private WebDriver driver;
private JavascriptExecutor jse;
private String aid = null;
/**
* 上傳附件
* @return
* @throws Exception
*/
public String uploadJPG() throws Exception{
//得到一個(gè)客戶(hù)端(用來(lái)發(fā)送請(qǐng)求的)
CloseableHttpClient client = HttpClients.createDefault();
//創(chuàng)建一個(gè)Post請(qǐng)求
HttpPost request = new HttpPost("http://192.168.1.8:8090/phpwind/index.php?c=upload&a=dorun&_json=1&_json=1");
//entity表示請(qǐng)求主體(body)
//StringEntity entity = null;
HttpEntity entity = null;
//response表示響應(yīng)
HttpResponse response = null;
//headers表示消息頭(response headers)
Header[] headers = null;
String aid = null;
try {
//設(shè)置請(qǐng)求消息主體的內(nèi)容
entity = MultipartEntityBuilder.create().
addTextBody("Filename", "mm02.jpg").
addTextBody("fid", "2").
addTextBody("Bzi_winduser","msL4udGA%2BPNSm7bGPAdLhpDKMwseQABD2Z6iMxwPN%2FrtT65uQOO6cw%3D%3D").
addTextBody("Bzi_visitor","s8hCqCZfxL3LMvGATleiatclmAgxUTQtW9P65vLo1aY%3D").
addTextBody("Bzi_lastvisit","132%091542205707%09%2Fphpwind%2Findex.php%3Fm%3Demotion%26type%3Dbbs%26csrf_token%3D8434d84b23423e26").
addTextBody("Bzi_visit_referer","fid_2_page_1").
addTextBody("Upload","Submit Query").
addTextBody("csrf_token","8434d84b23423e26").
addBinaryBody("Filedata", new File("C:\\Users\\yangzc\\Desktop\\temp05\\images\\mm02.jpg")).build();
request.setHeader("Cookie","csrf_token=8434d84b23423e26; Bzi_visit_referer=fid_2_page_1; Bzi_winduser=msL4udGA%2BPNSm7bGPAdLhpDKMwseQABD2Z6iMxwPN%2FrtT65uQOO6cw%3D%3D; Bzi_visitor=s8hCqCZfxL3LMvGATleiatclmAgxUTQtW9P65vLo1aY%3D; Bzi_lastvisit=132%091542205707%09%2Fphpwind%2Findex.php%3Fm%3Demotion%26type%3Dbbs%26csrf_token%3D8434d84b23423e26");
request.setHeader("X-Requested-With","ShockwaveFlash/31.0.0.148");
//將entity設(shè)置為post請(qǐng)求的主體
request.setEntity(entity);
//發(fā)送該post請(qǐng)求
response = httpClient.execute(request);
//
headers = response.getAllHeaders();
//輸出響應(yīng)消息頭的內(nèi)容
for(Header h:headers){
System.out.println(h.toString());
}
String responseBody = EntityUtils.toString(response.getEntity());
//輸出響應(yīng)消息主體的內(nèi)容
System.out.println(responseBody);
JSONObject obj = (JSONObject)JSON.parse(responseBody);
JSONObject data = (JSONObject)obj.get("data");
aid = (String)data.get("aid");
System.out.println("附件id是:" + aid);
} catch (Exception e) {
e.printStackTrace();
}
client.close();
return aid;
}
@BeforeMethod
public void begin() throws Exception{
//上傳附件
aid = uploadJPG();
//設(shè)置瀏覽器的路徑
//System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
//設(shè)置瀏覽器驅(qū)動(dòng)文件的路徑
//System.setProperty("webdriver.gecko.driver", "D:/Python27/geckodriver.exe");
//創(chuàng)建一個(gè)驅(qū)動(dòng)工具,用來(lái)操縱瀏覽器
//driver = new FirefoxDriver();
driver = new ChromeDriver();
//窗口最大化
driver.manage().window().maximize();
//設(shè)置隱性等待最大時(shí)長(zhǎng)
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
@Test
public void f() throws Exception {
driver.get("http://192.168.1.8:8090/phpwind/index.php?m=u&c=login");
//driver.findElement(By.id("J_u_login_username")).sendKeys("zPmuGU43QiTyxCH");
driver.findElement(By.id("J_u_login_username")).sendKeys("jojo");
//
driver.findElement(By.id("J_u_login_password")).sendKeys("123456");
//
driver.findElement(By.className("btn_big")).click();
//
Thread.sleep(2000);
//
driver.findElement(By.cssSelector("#J_head_forum_post > span > span")).click();
//
driver.findElement(By.xpath("http://*[@id='J_forum_list']/li[1]")).click();
//
driver.findElement(By.xpath("http://*[@id='J_forum_ul']/li[1]")).click();
//
driver.findElement(By.id("J_head_forum_sub")).click();
//
driver.findElement(By.id("J_atc_title")).sendKeys("附件編號(hào)"+aid+"這是一個(gè)標(biāo)題112233");
//
driver.switchTo().defaultContent();
//切換到iframe框架
driver.switchTo().frame(driver.findElement(By.className("wind_editor_iframe")));
//
driver.findElement(By.className("editor_content")).sendKeys("附件編號(hào)"+aid+"這是帖子的內(nèi)容112233");
String script = "$('#mainForm').append(\"<input type='text' name='flashatt["+aid+"][desc]'>\");"
+ "$('#mainForm').append(\"<input type='text' name='flashatt["+aid+"][cost]'>\");"
+ "$('#mainForm').append(\"<input type='text' name='flashatt["+aid+"][ctype]' value='1'>\");";
/*
String script = "var parent = document.getElementById(\"mainForm\");"
+ "var child1 = document.createElement(\"input\");"
+ "var child2 = document.createElement(\"input\");"
+ "var child3 = document.createElement(\"input\");"
+ "child1.setAttribute(\"type\",\"text\");"
+ "child1.setAttribute(\"name\",\"flashatt[17][desc]\");"
+ "child2.setAttribute(\"type\",\"text\");"
+ "child2.setAttribute(\"name\",\"flashatt[17][cost]\");"
+ "child3.setAttribute(\"type\",\"text\");"
+ "child3.setAttribute(\"name\",\"flashatt[17][ctype]\");"
+ "child3.setAttribute(\"value\",\"1\");"
+ "parent.appendChild(child1);"
+ "parent.appendChild(child2);"
+ "parent.appendChild(child3);";
*/
System.out.println(script);
Thread.sleep(1000);
driver.switchTo().defaultContent();
jse = (JavascriptExecutor) driver;
//
injectjQueryIfNeeded();
Thread.sleep(1000);
jse.executeScript(script);
//
driver.findElement(By.id("J_post_sub")).click();
Thread.sleep(3000);
}
@AfterMethod
public void end(){
driver.quit();
}
private void injectjQueryIfNeeded() {
if (!jQueryLoaded())
injectjQuery();
}
// 判斷是已加載jQuery
public Boolean jQueryLoaded() {
Boolean loaded;
try {
loaded = (Boolean) jse.executeScript("return " + "jQuery()!=null");
} catch (WebDriverException e) {
loaded = false;
}
return loaded;
}
//通過(guò)注入jQuery
public void injectjQuery() {
jse.executeScript(" var headID = "
+ "document.getElementsByTagName(\"head\")[0];"
+ "var newScript = document.createElement('script');"
+ "newScript.type = 'text/javascript';" + "newScript.src = "
+ "'http://192.168.1.8:8090"
+ "/phpwind/res/js/dev/jquery.js?v=20161021';"
+ "headID.appendChild(newScript);");
}
}
執(zhí)行自動(dòng)化腳本
webdriver1114_02.gif
參考資料
[01] selenium 加載jquery
https://www.cnblogs.com/x666-6/p/8406436.html
[02] Flash上傳組件之SWFUpload文件上傳
https://www.cnblogs.com/ziranfengwei/p/7462808.html
[03] 文件上傳利器SWFUpload入門(mén)簡(jiǎn)易教程
https://www.cnblogs.com/diycp/articles/3497876.html
微信掃一掃關(guān)注該公眾號(hào)【測(cè)試開(kāi)發(fā)者部落】
image.png
點(diǎn)擊鏈接加入群聊【軟件測(cè)試學(xué)習(xí)交流群】
https://jq.qq.com/?_wv=1027&k=5eVEhfN
軟件測(cè)試學(xué)習(xí)交流QQ群號(hào):511619105