Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/upload_test.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
'use strict'
19
20
const assert = require('node:assert')
21
const fs = require('node:fs')
22
const io = require('selenium-webdriver/io')
23
const remote = require('selenium-webdriver/remote')
24
const test = require('../lib/test')
25
const { Browser, By, until } = require('selenium-webdriver')
26
27
const Pages = test.Pages
28
29
test.suite(function (env) {
30
var LOREM_IPSUM_TEXT = 'lorem ipsum dolor sit amet'
31
var FILE_HTML = '<!DOCTYPE html><div>' + LOREM_IPSUM_TEXT + '</div>'
32
var FILE_HTML_2 = '<!DOCTYPE html><div>' + 'I love sausages too' + '</div>'
33
34
var _fp
35
before(function () {
36
return (_fp = io.tmpFile().then(function (fp) {
37
fs.writeFileSync(fp, FILE_HTML)
38
return fp
39
}))
40
})
41
42
var driver
43
before(async function () {
44
driver = await env.builder().build()
45
})
46
47
after(function () {
48
if (driver) {
49
return driver.quit()
50
}
51
})
52
53
test.ignore(env.browsers(Browser.SAFARI)).it('can upload multiple files', async function () {
54
driver.setFileDetector(new remote.FileDetector())
55
56
await driver.get(Pages.uploadPage)
57
58
var fp1 = await io.tmpFile().then(function (fp) {
59
fs.writeFileSync(fp, FILE_HTML)
60
return fp
61
})
62
63
var fp2 = await io.tmpFile().then(function (fp) {
64
fs.writeFileSync(fp, FILE_HTML_2)
65
return fp
66
})
67
68
await driver.findElement(By.id('upload')).sendKeys(fp1 + '\n' + fp2)
69
await driver.findElement(By.id('go')).click()
70
71
// Uploading files across a network may take a while, even if they're small.
72
var label = await driver.findElement(By.id('upload_label'))
73
await driver.wait(until.elementIsNotVisible(label), 10 * 1000, 'File took longer than 10 seconds to upload!')
74
75
var frame = await driver.findElement(By.id('upload_target'))
76
await driver.switchTo().frame(frame)
77
const txt = await driver.findElement(By.css('body')).getText()
78
79
assert.match(txt, new RegExp(fp1.split('/').pop())) // eslint-disable-line
80
assert.match(txt, new RegExp(fp2.split('/').pop())) // eslint-disable-line
81
})
82
83
test.ignore(env.browsers(Browser.SAFARI)).it('can upload files', async function () {
84
driver.setFileDetector(new remote.FileDetector())
85
86
await driver.get(Pages.uploadPage)
87
88
var fp = await io.tmpFile().then(function (fp) {
89
fs.writeFileSync(fp, FILE_HTML)
90
return fp
91
})
92
93
await driver.findElement(By.id('upload')).sendKeys(fp)
94
await driver.findElement(By.id('go')).click()
95
// Uploading files across a network may take a while, even if they're small.
96
var label = await driver.findElement(By.id('upload_label'))
97
await driver.wait(until.elementIsNotVisible(label), 10 * 1000, 'File took longer than 10 seconds to upload!')
98
99
var frame = await driver.findElement(By.id('upload_target'))
100
await driver.switchTo().frame(frame)
101
const txt = await driver.findElement(By.css('body')).getText()
102
assert.strictEqual(txt, fp.split('/').pop(), `The document contained ${await driver.getPageSource()}`)
103
})
104
})
105
106