如何使用Winium将文件上传到Web浏览器?

时间:2016-07-01 08:56:24

标签: java selenium

我知道我可以通过多种方式将文件上传到浏览器,例如:AutoIt,Robot Class和其他方式(我尝试了所有这些并且大部分时间都在工作)。

我介绍了Winium,我想用它做同样的测试用例,也就是说,使用它将文件上传到浏览器,但我不知道如何在web驱动程序和winium驱动程序之间切换。请帮助,因为我搜索了很多这个技巧,但找不到任何结果

package testUtilities;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.winium.WiniumDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class WiniumWeb 
{
    WebDriver driver;

    @BeforeClass
    public void setUp() throws IOException
    {
        driver = new FirefoxDriver();
        driver.navigate().to("http://the-internet.herokuapp.com/upload");
        driver.findElement(By.id("file-upload")).click();

        String WiniumEXEpath = System.getProperty("user.dir") + "\\Resources\\Winium.Desktop.Driver.exe";
        File file = new File(WiniumEXEpath);
        if (! file.exists()) 
        {
           throw new IllegalArgumentException("The file " + WiniumEXEpath + " does not exist");
        }
        Runtime.getRuntime().exec(file.getAbsolutePath());



        try 
        {
            driver = new WiniumDriver(new URL("http://localhost:9999"), null);
        } catch (MalformedURLException e) 
        {
            e.printStackTrace();
        }

    }

    @Test
    public void testNotePade() throws InterruptedException
    {
        String file = System.getProperty("user.dir") + "\\Resources\\TestData.csv";
        WebElement window = driver.findElement(By.className("File Upload")); 
        window.findElement(By.className("#32770")).sendKeys(file);

        Thread.sleep(2000);
    }


}

2 个答案:

答案 0 :(得分:0)

如果你还在寻找解决方案。我正在分享我的脚本,这对我有用。

public class FileUpload extends BaseClass {
static WiniumDriver d;

@BeforeClass
public void setUp() throws IOException {
    DesktopOptions options = new DesktopOptions();
    options.setApplicationPath("C:\\Windows\\System32\\openfiles.exe");
    LaunchLocalBrowser("chrome","http://the-internet.herokuapp.com/upload");//use your own code to launch browser
    driver.findElement(By.id("file-upload")).click();

    String WiniumEXEpath = System.getProperty("user.dir") + "\\lib\\Winium.Desktop.Driver.exe";
    File file = new File(WiniumEXEpath);
    if (! file.exists()) {
        throw new IllegalArgumentException("The file " + WiniumEXEpath + " does not exist");
    }
    Runtime.getRuntime().exec(file.getAbsolutePath());
    try {
        d = new WiniumDriver(new URL("http://localhost:9999"),options);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}

@Test
public void testNotePade() throws InterruptedException {
    String file = System.getProperty("user.dir") + "\\lib\\Testdata.txt";
    d.findElementByName("File name:").sendKeys(file);
    d.findElementByXPath("//*[@Name='Cancel']//preceding-sibling::*[@Name='Open']").click();
    driver.findElement(By.id("file-submit")).click(); 
}
}

答案 1 :(得分:0)

使用 WiniumDriverService 上传文件,方法是参考端口9999。使用有问题的空闲端口创建winium实例。以下代码旨在用于实现浏览器工厂的示例实现,以简化Web和桌面实例。

public class FactoryManager {

    public static ClientFactory getIndividualProduct(EnumProductLists product) {
        ClientFactory factory = null;
        if (null != product) {
            switch (product) {
            case CHROME:
                factory = new ProductChromeClient();
                break;
            case DESKTOP:
                factory = new ProductWiniumClient();
                break;

            default:
                break;
            }
        }
        return factory;
    }
}

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.winium.DesktopOptions;
import org.openqa.selenium.winium.WiniumDriver;
import org.openqa.selenium.winium.WiniumDriverService;

public class ProductWiniumClient extends ClientFactory {
    private WiniumDriverService service;
    @Override
    protected void startService() {
        if (null == service) {
            service = new WiniumDriverService.Builder()
                    .usingDriverExecutable(
                            new File(System.getProperty("user.dir") + "/WiniumFolder/Winium.Desktop.Driver.exe"))
                    .usingPort(9999).withVerbose(true).buildDesktopService();
            try {
                service.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void createService() {
        startService();
        DesktopOptions options = new DesktopOptions();
        options.setApplicationPath("C:\\Windows\\System32\\openfiles.exe");
        deskClient = new WiniumDriver(service, options);
    }

    @Override
    protected void stopService() {
        if (null != service && service.isRunning()) {
            service.stop();
        }
    }
}
public class TestCase1 {

    WebDriver webClient;
    WiniumDriver deskClient;
    ClientFactory lists;
    @BeforeTest
    public void beforeTest() {
        lists = FactoryManager.getIndividualProduct(EnumProductLists.CHROME);
        webClient = (WebDriver) this.lists.getClient(WebDriver.class.getTypeName());
        lists = FactoryManager.getIndividualProduct(EnumProductLists.DESKTOP);
        deskClient = (WiniumDriver) this.lists.getClient("");
    }
    @Test
    public void f() {
        if (null != webClient) {
            try {
                webClient.manage().window().maximize();
                webClient.get("https://uploadfiles.io/");
                webClient.findElement(By.id("upload-window")).click();
                String file = System.getProperty("user.dir") + "\\files\\upload.txt";
                deskClient.findElement(By.name("File name:")).sendKeys(file);
                deskClient.findElement(By.xpath("//*[@Name='Cancel']//preceding-sibling::*[@Name='Open']")).click();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Client Instance is Null!");
        }
    }
    @AfterTest
    public void afterTest() {
    }
}