从Android手机在Chrome上上传硒文件

时间:2018-06-23 11:51:03

标签: selenium selenium-webdriver appium

我正在尝试在Android手机上的chrome浏览器上运行的硒测试中上传文件。通常的element.sendkeys(path)无法正常工作。 是否有任何已知的android替代品?

1 个答案:

答案 0 :(得分:0)

上传文件可能会有所帮助:

public CorePage UploadHack(string fileInputId, string contentType, string fileContent, string fileName, string angularScopeVar)
    {
        UploadFile(_filePath);
        var uploadHack =
            "(function(){" +
                "function convert(base64){" +
                    "var raw = atob(base64);" +
                    "var arr = new Uint8Array(new ArrayBuffer(raw.length));" +
                    "for (var i = 0; i < raw.length; i++){" +
                        "arr[i] = raw.charCodeAt(i);" +
                    "}" +
                    "return arr; " +
                "}" +
                $"var file = new Blob([convert('{fileContent}')], {{'type':'{contentType}'}}); " +
                $"file.name = '{fileName}'; " +
                $"angular.element('#{fileInputId}').scope().{angularScopeVar} = file;" +
            "})()";
        Driver.ExecuteJavaScript(uploadHack);
        return this;
    }