Skip to content

Commit ea99a3e

Browse files
author
David Haeffner
committed
Made it so legacy test cases and suites without a base URL can be imported. Fixes #486
1 parent dc0b282 commit ea99a3e

File tree

4 files changed

+619
-543
lines changed

4 files changed

+619
-543
lines changed

packages/selenium-ide/src/neo/IO/filesystem.js

Lines changed: 134 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -15,167 +15,196 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
import browser from "webextension-polyfill";
19-
import parser from "ua-parser-js";
20-
import { js_beautify as beautify } from "js-beautify";
21-
import UpgradeProject from "./migrate";
22-
import { verifyFile, FileTypes, migrateTestCase, migrateProject, migrateUrls } from "./legacy/migrate";
23-
import TestCase from "../models/TestCase";
24-
import UiState from "../stores/view/UiState";
25-
import PlaybackState from "../stores/view/PlaybackState";
26-
import ModalState from "../stores/view/ModalState";
27-
import Selianize, { ParseError } from "selianize";
28-
import Manager from "../../plugin/manager";
29-
import chromeGetFile from "./filesystem/chrome";
30-
import firefoxGetFile from "./filesystem/firefox";
31-
32-
export const supportedFileFormats = ".side, text/html";
33-
const parsedUA = parser(window.navigator.userAgent);
18+
import browser from 'webextension-polyfill'
19+
import { js_beautify as beautify } from 'js-beautify'
20+
import UpgradeProject from './migrate'
21+
import {
22+
verifyFile,
23+
FileTypes,
24+
migrateTestCase,
25+
migrateProject,
26+
migrateUrls,
27+
} from './legacy/migrate'
28+
import TestCase from '../models/TestCase'
29+
import UiState from '../stores/view/UiState'
30+
import PlaybackState from '../stores/view/PlaybackState'
31+
import ModalState from '../stores/view/ModalState'
32+
import Selianize, { ParseError } from 'selianize'
33+
import Manager from '../../plugin/manager'
34+
import chromeGetFile from './filesystem/chrome'
35+
import firefoxGetFile from './filesystem/firefox'
36+
import { userAgent as parsedUA } from '../../common/utils'
37+
export const supportedFileFormats = '.side, text/html'
3438

3539
export function getFile(path) {
36-
const browserName = parsedUA.browser.name;
40+
const browserName = parsedUA.browser.name
3741
return (() => {
38-
if (browserName === "Chrome") {
39-
return chromeGetFile(path);
40-
} else if (browserName === "Firefox") {
41-
return firefoxGetFile(path);
42+
if (browserName === 'Chrome') {
43+
return chromeGetFile(path)
44+
} else if (browserName === 'Firefox') {
45+
return firefoxGetFile(path)
4246
} else {
43-
return Promise.reject(new Error("Operation is not supported in this browser"));
47+
return Promise.reject(
48+
new Error('Operation is not supported in this browser')
49+
)
4450
}
4551
})().then(blob => {
46-
return new Promise((res) => {
47-
const reader = new FileReader();
48-
reader.addEventListener("load", () => {
49-
res(reader.result);
50-
});
51-
reader.readAsDataURL(blob);
52-
});
53-
});
52+
return new Promise(res => {
53+
const reader = new FileReader()
54+
reader.addEventListener('load', () => {
55+
res(reader.result)
56+
})
57+
reader.readAsDataURL(blob)
58+
})
59+
})
5460
}
5561

5662
export function loadAsText(blob) {
57-
return new Promise((res) => {
58-
const fileReader = new FileReader();
59-
fileReader.onload = (e) => {
60-
res(e.target.result);
61-
};
63+
return new Promise(res => {
64+
const fileReader = new FileReader()
65+
fileReader.onload = e => {
66+
res(e.target.result)
67+
}
6268

63-
fileReader.readAsText(blob);
64-
});
69+
fileReader.readAsText(blob)
70+
})
6571
}
6672

6773
export function saveProject(_project) {
68-
const project = _project.toJS();
69-
downloadProject(project);
70-
UiState.saved();
74+
const project = _project.toJS()
75+
downloadProject(project)
76+
UiState.saved()
7177
}
7278

7379
function downloadProject(project) {
7480
return exportProject(project).then(snapshot => {
7581
if (snapshot) {
76-
project.snapshot = snapshot;
77-
Object.assign(project, Manager.emitDependencies());
82+
project.snapshot = snapshot
83+
Object.assign(project, Manager.emitDependencies())
7884
}
7985
return browser.downloads.download({
80-
filename: project.name + ".side",
81-
url: createBlob("application/json", beautify(JSON.stringify(project), { indent_size: 2 })),
86+
filename: project.name + '.side',
87+
url: createBlob(
88+
'application/json',
89+
beautify(JSON.stringify(project), { indent_size: 2 })
90+
),
8291
saveAs: true,
83-
conflictAction: "overwrite"
84-
});
85-
});
92+
conflictAction: 'overwrite',
93+
})
94+
})
8695
}
8796

8897
function exportProject(project) {
8998
return Manager.validatePluginExport(project).then(() => {
90-
return Selianize(project, { silenceErrors: true, skipStdLibEmitting: true }).catch(err => {
91-
const markdown = ParseError(err && err.message || err);
92-
ModalState.showAlert({
93-
title: "Error saving project",
94-
description: markdown,
95-
confirmLabel: "Download log",
96-
cancelLabel: "Close"
97-
}, (choseDownload) => {
98-
if (choseDownload) {
99-
browser.downloads.download({
100-
filename: project.name + "-logs.md",
101-
url: createBlob("text/markdown", markdown),
102-
saveAs: true,
103-
conflictAction: "overwrite"
104-
});
99+
return Selianize(project, {
100+
silenceErrors: true,
101+
skipStdLibEmitting: true,
102+
}).catch(err => {
103+
const markdown = ParseError((err && err.message) || err)
104+
ModalState.showAlert(
105+
{
106+
title: 'Error saving project',
107+
description: markdown,
108+
confirmLabel: 'Download log',
109+
cancelLabel: 'Close',
110+
},
111+
choseDownload => {
112+
if (choseDownload) {
113+
browser.downloads.download({
114+
filename: project.name + '-logs.md',
115+
url: createBlob('text/markdown', markdown),
116+
saveAs: true,
117+
conflictAction: 'overwrite',
118+
})
119+
}
105120
}
106-
});
107-
return Promise.reject();
108-
});
109-
});
121+
)
122+
return Promise.reject()
123+
})
124+
})
110125
}
111126

112-
let previousFile = null;
127+
let previousFile = null
128+
// eslint-disable-next-line
113129
function createBlob(mimeType, data) {
114130
const blob = new Blob([data], {
115-
type: "text/plain"
116-
});
131+
type: 'text/plain',
132+
})
117133
// If we are replacing a previously generated file we need to
118134
// manually revoke the object URL to avoid memory leaks.
119135
if (previousFile !== null) {
120-
window.URL.revokeObjectURL(previousFile);
136+
window.URL.revokeObjectURL(previousFile)
121137
}
122-
previousFile = window.URL.createObjectURL(blob);
123-
return previousFile;
138+
previousFile = window.URL.createObjectURL(blob)
139+
return previousFile
124140
}
125141

126142
export function loadProject(project, file) {
127143
function displayError(error) {
128144
ModalState.showAlert({
129-
title: "Error migrating project",
145+
title: 'Error migrating project',
130146
description: error.message,
131-
confirmLabel: "Close"
132-
});
147+
confirmLabel: 'Close',
148+
})
133149
}
134-
loadAsText(file).then((contents) => {
150+
loadAsText(file).then(contents => {
135151
if (/\.side$/.test(file.name)) {
136-
loadJSProject(project, UpgradeProject(JSON.parse(contents)));
152+
loadJSProject(project, UpgradeProject(JSON.parse(contents)))
137153
} else {
138154
try {
139-
const type = verifyFile(contents);
155+
const type = verifyFile(contents)
140156
if (type === FileTypes.Suite) {
141-
ModalState.importSuite(contents, (files) => {
142-
loadJSProject(project, migrateProject(files));
143-
});
157+
ModalState.importSuite(contents, files => {
158+
try {
159+
loadJSProject(project, migrateProject(files))
160+
} catch (error) {
161+
displayError(error)
162+
}
163+
})
144164
} else if (type === FileTypes.TestCase) {
145-
const { test, baseUrl } = migrateTestCase(contents);
146-
if (!project.urls.includes(baseUrl)) {
147-
ModalState.showAlert({
148-
title: "Migrate test case",
149-
description: `The test case you're trying to migrate has a different base URL (${baseUrl}) than the project's one. \nIn order to migrate the test case URLs will be made absolute.`,
150-
confirmLabel: "Migrate",
151-
cancelLabel: "Discard"
152-
}, (choseMigration) => {
153-
if (choseMigration) {
154-
UiState.selectTest(project.addTestCase(TestCase.fromJS(migrateUrls(test, baseUrl))));
165+
let { test, baseUrl } = migrateTestCase(contents)
166+
if (project.urls.length && !project.urls.includes(baseUrl)) {
167+
ModalState.showAlert(
168+
{
169+
title: 'Migrate test case',
170+
description: `The test case you're trying to migrate has a different base URL (${baseUrl}) than the project's one. \nIn order to migrate the test case URLs will be made absolute.`,
171+
confirmLabel: 'Migrate',
172+
cancelLabel: 'Discard',
173+
},
174+
choseMigration => {
175+
if (choseMigration) {
176+
UiState.selectTest(
177+
project.addTestCase(
178+
TestCase.fromJS(migrateUrls(test, baseUrl))
179+
)
180+
)
181+
}
155182
}
156-
});
183+
)
157184
} else {
158-
UiState.selectTest(project.addTestCase(TestCase.fromJS(test, baseUrl)));
185+
UiState.selectTest(
186+
project.addTestCase(TestCase.fromJS(test, baseUrl))
187+
)
159188
}
160189
}
161190
} catch (error) {
162-
displayError(error);
191+
displayError(error)
163192
}
164193
}
165-
});
194+
})
166195
}
167196

168197
export function loadJSProject(project, data) {
169-
UiState.changeView("Tests");
170-
PlaybackState.clearPlayingCache();
171-
UiState.clearViewCache();
172-
project.fromJS(data);
173-
UiState.projectChanged();
198+
UiState.changeView('Tests')
199+
PlaybackState.clearPlayingCache()
200+
UiState.clearViewCache()
201+
project.fromJS(data)
202+
UiState.projectChanged()
174203
Manager.emitMessage({
175-
action: "event",
176-
event: "projectLoaded",
204+
action: 'event',
205+
event: 'projectLoaded',
177206
options: {
178-
projectName: project.name
179-
}
180-
});
207+
projectName: project.name,
208+
},
209+
})
181210
}

0 commit comments

Comments
 (0)