Path: blob/trunk/javascript/selenium-webdriver/test/upload_test.js
3987 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const fs = require('node:fs')21const path = require('node:path')22const io = require('selenium-webdriver/io')23const remote = require('selenium-webdriver/remote')24const test = require('../lib/test')25const { Browser, By, until } = require('selenium-webdriver')2627const Pages = test.Pages2829test.suite(function (env) {30var LOREM_IPSUM_TEXT = 'lorem ipsum dolor sit amet'31var FILE_HTML = '<!DOCTYPE html><div>' + LOREM_IPSUM_TEXT + '</div>'32var FILE_HTML_2 = '<!DOCTYPE html><div>' + 'I love sausages too' + '</div>'3334var _fp35before(function () {36return (_fp = io.tmpFile().then(function (fp) {37fs.writeFileSync(fp, FILE_HTML)38return fp39}))40})4142var driver43before(async function () {44driver = await env.builder().build()45})4647after(function () {48if (driver) {49return driver.quit()50}51})5253test.ignore(env.browsers(Browser.SAFARI)).it('can upload multiple files', async function () {54driver.setFileDetector(new remote.FileDetector())5556await driver.get(Pages.uploadPage)5758var fp1 = await io.tmpFile().then(function (fp) {59fs.writeFileSync(fp, FILE_HTML)60return fp61})6263var fp2 = await io.tmpFile().then(function (fp) {64fs.writeFileSync(fp, FILE_HTML_2)65return fp66})6768await driver.findElement(By.id('upload')).sendKeys(fp1 + '\n' + fp2)69await driver.findElement(By.id('go')).click()7071// Uploading files across a network may take a while, even if they're small.72var label = await driver.findElement(By.id('upload_label'))73await driver.wait(until.elementIsNotVisible(label), 10 * 1000, 'File took longer than 10 seconds to upload!')7475var frame = await driver.findElement(By.id('upload_target'))76await driver.switchTo().frame(frame)77const txt = await driver.findElement(By.css('body')).getText()7879assert.ok(txt.includes(path.basename(fp1)))80assert.ok(txt.includes(path.basename(fp2)))81})8283test.ignore(env.browsers(Browser.SAFARI)).it('can upload files', async function () {84driver.setFileDetector(new remote.FileDetector())8586await driver.get(Pages.uploadPage)8788var fp = await io.tmpFile().then(function (fp) {89fs.writeFileSync(fp, FILE_HTML)90return fp91})9293await driver.findElement(By.id('upload')).sendKeys(fp)94await driver.findElement(By.id('go')).click()95// Uploading files across a network may take a while, even if they're small.96var label = await driver.findElement(By.id('upload_label'))97await driver.wait(until.elementIsNotVisible(label), 10 * 1000, 'File took longer than 10 seconds to upload!')9899var frame = await driver.findElement(By.id('upload_target'))100await driver.switchTo().frame(frame)101const txt = await driver.findElement(By.css('body')).getText()102assert.strictEqual(txt, path.basename(fp), `The document contained ${await driver.getPageSource()}`)103})104})105106107