Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/javascript/selenium-webdriver/test/upload_test.js
3987 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 path = require('node:path')
23
const io = require('selenium-webdriver/io')
24
const remote = require('selenium-webdriver/remote')
25
const test = require('../lib/test')
26
const { Browser, By, until } = require('selenium-webdriver')
27
28
const Pages = test.Pages
29
30
test.suite(function (env) {
31
var LOREM_IPSUM_TEXT = 'lorem ipsum dolor sit amet'
32
var FILE_HTML = '<!DOCTYPE html><div>' + LOREM_IPSUM_TEXT + '</div>'
33
var FILE_HTML_2 = '<!DOCTYPE html><div>' + 'I love sausages too' + '</div>'
34
35
var _fp
36
before(function () {
37
return (_fp = io.tmpFile().then(function (fp) {
38
fs.writeFileSync(fp, FILE_HTML)
39
return fp
40
}))
41
})
42
43
var driver
44
before(async function () {
45
driver = await env.builder().build()
46
})
47
48
after(function () {
49
if (driver) {
50
return driver.quit()
51
}
52
})
53
54
test.ignore(env.browsers(Browser.SAFARI)).it('can upload multiple files', async function () {
55
driver.setFileDetector(new remote.FileDetector())
56
57
await driver.get(Pages.uploadPage)
58
59
var fp1 = await io.tmpFile().then(function (fp) {
60
fs.writeFileSync(fp, FILE_HTML)
61
return fp
62
})
63
64
var fp2 = await io.tmpFile().then(function (fp) {
65
fs.writeFileSync(fp, FILE_HTML_2)
66
return fp
67
})
68
69
await driver.findElement(By.id('upload')).sendKeys(fp1 + '\n' + fp2)
70
await driver.findElement(By.id('go')).click()
71
72
// Uploading files across a network may take a while, even if they're small.
73
var label = await driver.findElement(By.id('upload_label'))
74
await driver.wait(until.elementIsNotVisible(label), 10 * 1000, 'File took longer than 10 seconds to upload!')
75
76
var frame = await driver.findElement(By.id('upload_target'))
77
await driver.switchTo().frame(frame)
78
const txt = await driver.findElement(By.css('body')).getText()
79
80
assert.ok(txt.includes(path.basename(fp1)))
81
assert.ok(txt.includes(path.basename(fp2)))
82
})
83
84
test.ignore(env.browsers(Browser.SAFARI)).it('can upload files', async function () {
85
driver.setFileDetector(new remote.FileDetector())
86
87
await driver.get(Pages.uploadPage)
88
89
var fp = await io.tmpFile().then(function (fp) {
90
fs.writeFileSync(fp, FILE_HTML)
91
return fp
92
})
93
94
await driver.findElement(By.id('upload')).sendKeys(fp)
95
await driver.findElement(By.id('go')).click()
96
// Uploading files across a network may take a while, even if they're small.
97
var label = await driver.findElement(By.id('upload_label'))
98
await driver.wait(until.elementIsNotVisible(label), 10 * 1000, 'File took longer than 10 seconds to upload!')
99
100
var frame = await driver.findElement(By.id('upload_target'))
101
await driver.switchTo().frame(frame)
102
const txt = await driver.findElement(By.css('body')).getText()
103
assert.strictEqual(txt, path.basename(fp), `The document contained ${await driver.getPageSource()}`)
104
})
105
})
106
107