首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Winium将文件上传到web浏览器?

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

Stack Overflow用户
提问于 2016-07-01 16:56:24
回答 2查看 2.2K关注 0票数 0

我知道我可以通过许多方式将文件上传到浏览器,例如: AutoIt、Robot Class和其他方式(我尝试了所有这些方法,它们大部分时间都有效)。

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

代码语言:javascript
复制
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);
    }


}
EN

回答 2

Stack Overflow用户

发布于 2017-12-01 21:12:20

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

代码语言:javascript
复制
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(); 
}
}
票数 0
EN

Stack Overflow用户

发布于 2019-04-11 17:06:34

通过引用端口9999使用WiniumDriverService上传文件。使用空闲端口创建winium实例时出现问题。以下代码用于浏览器工厂的示例实现,以促进web和桌面实例。

代码语言:javascript
复制
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() {
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38141171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档