简体   繁体   中英

How to perform a submit action automatically in an external site

that's my problem:

Everyday I visit a website (with login of course) and upload the same image. I'd like to know if there is a way to automatize this process.

I hope someone cane help me :) Thanks for your help!

You can try submitting files through curl.

 $file_name_with_full_path = realpath('./sample.jpeg');
 $post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$target_url);
 curl_setopt($ch, CURLOPT_POST,1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
 $result=curl_exec ($ch);
 curl_close ($ch);

And to login to a specific website and keep cookies you can make a function similar to this:

public static function curlResult ($url, $getHeaders = false, $data) {

    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1",
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        CURLOPT_COOKIESESSION  => true,
        CURLOPT_COOKIEJAR      => dirname(__FILE__) . '/cookie.txt',
        CURLOPT_COOKIEFILE     => dirname(__FILE__) . '/cookie.txt'
    );

    $ch = curl_init( $url );
    curl_setopt_array( $ch, $options );

    if ($data != false) {
       curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
       curl_setopt($ch, CURLOPT_POST, $data?1:0);
    }

    $content = curl_exec( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    if ($getHeaders) {
        return $header;
    }

    return $content;
}

Then use the function something like:

// Login 
curlResult('urltologin', false, array('username' => 'test', 'password' => 'demo');
// Upload
curlResult('urltoupload', false, array(file_contents'=>'@'.$file_name_with_full_path));

Alternatively, you could use Selenium (through their own Java Client ):

Taken from here :

import junit.framework.Assert;
import junit.framework.TestCase;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;
import java.net.URL;
import java.util.concurrent.TimeUnit;
    public class TestingUploadSe2Sauce extends TestCase {
    private RemoteWebDriver driver;
    public void setUp() throws Exception {
            DesiredCapabilities capabillities = DesiredCapabilities.firefox();
            capabillities.setCapability("version", "7");
            capabillities.setCapability("platform", Platform.XP);
            capabillities.setCapability("selenium-version", "2.18.0");
            capabillities.setCapability("name", "Remote File Upload using Selenium 2's FileDetectors");
        driver = new RemoteWebDriver(
            new URL("http://<username>:<api-key>@ondemand.saucelabs.com:80/wd/hub"),
        capabillities);
        driver.setFileDetector(new LocalFileDetector());
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

public void testSauce() throws Exception {
        driver.get("http://sso.dev.saucelabs.com/test/guinea-file-upload");
        WebElement upload = driver.findElement(By.id("myfile"));
        upload.sendKeys("/Users/sso/the/local/path/to/darkbulb.jpg");
        driver.findElement(By.id("submit")).click();
        driver.findElement(By.tagName("img"));
        Assert.assertEquals("darkbulb.jpg (image/jpeg)", driver.findElement(By.tagName("p")).getText());
}
public void tearDown() throws Exception {
      driver.quit();

}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM