扫码关注本人公众号
image.png另一种思路(针对swfupload上传插件)
1、使用httpclient上传附件
2、调用JS脚本,往表单里面添加跟附件有关的输入项
image.png
设计上传图片的脚本
代码如下:
/**
* 上传附件
* @return
* @throws Exception
*/
public String uploadJPG() throws Exception{
//得到一个客户端(用来发送请求的)
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个Post请求
HttpPost httpPost = 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;
//httpResponse表示响应
HttpResponse httpResponse = null;
//headers表示消息头(response headers)
Header[] headers = null;
String aid = null;
try {
//设置请求消息主体的内容
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();
httpPost.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");
httpPost.setHeader("X-Requested-With","ShockwaveFlash/31.0.0.148");
//将entity设置为post请求的主体
httpPost.setEntity(entity);
//发送该post请求
httpResponse = httpClient.execute(httpPost);
//
headers = httpResponse.getAllHeaders();
//输出响应消息头的内容
for(Header h:headers){
System.out.println(h.toString());
}
String responseContent = EntityUtils.toString(httpResponse.getEntity());
//输出响应消息主体的内容
System.out.println(responseContent);
JSONObject obj = (JSONObject)JSON.parse(responseContent);
JSONObject data = (JSONObject)obj.get("data");
aid = (String)data.get("aid");
System.out.println("附件id是:" + aid);
} catch (Exception e) {
e.printStackTrace();
}
httpClient.close();
return aid;
}
往表单里面添加跟附件有关的输入项
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{
//得到一个客户端(用来发送请求的)
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个Post请求
HttpPost httpPost = 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;
//httpResponse表示响应
HttpResponse httpResponse = null;
//headers表示消息头(response headers)
Header[] headers = null;
String aid = null;
try {
//设置请求消息主体的内容
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();
httpPost.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");
httpPost.setHeader("X-Requested-With","ShockwaveFlash/31.0.0.148");
//将entity设置为post请求的主体
httpPost.setEntity(entity);
//发送该post请求
httpResponse = httpClient.execute(httpPost);
//
headers = httpResponse.getAllHeaders();
//输出响应消息头的内容
for(Header h:headers){
System.out.println(h.toString());
}
String responseContent = EntityUtils.toString(httpResponse.getEntity());
//输出响应消息主体的内容
System.out.println(responseContent);
JSONObject obj = (JSONObject)JSON.parse(responseContent);
JSONObject data = (JSONObject)obj.get("data");
aid = (String)data.get("aid");
System.out.println("附件id是:" + aid);
} catch (Exception e) {
e.printStackTrace();
}
httpClient.close();
return aid;
}
@BeforeMethod
public void begin() throws Exception{
//上传附件
aid = uploadJPG();
//设置浏览器的路径
//System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
//设置浏览器驱动文件的路径
//System.setProperty("webdriver.gecko.driver", "D:/Python27/geckodriver.exe");
//创建一个驱动工具,用来操纵浏览器
//driver = new FirefoxDriver();
driver = new ChromeDriver();
//窗口最大化
driver.manage().window().maximize();
//设置隐性等待最大时长
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("//*[@id='J_forum_list']/li[1]")).click();
//
driver.findElement(By.xpath("//*[@id='J_forum_ul']/li[1]")).click();
//
driver.findElement(By.id("J_head_forum_sub")).click();
//
driver.findElement(By.id("J_atc_title")).sendKeys("附件编号"+aid+"这是一个标题112233");
//
driver.switchTo().defaultContent();
//切换到iframe框架
driver.switchTo().frame(driver.findElement(By.className("wind_editor_iframe")));
//
driver.findElement(By.className("editor_content")).sendKeys("附件编号"+aid+"这是帖子的内容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;
}
//通过注入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);");
}
}
网友评论