Skip to content

Commit

Permalink
- fixed element fetching
Browse files Browse the repository at this point in the history
- add authentication to _navigateTo
  • Loading branch information
tnobody committed Mar 1, 2019
1 parent e9734c1 commit dae0da2
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 47 deletions.
14 changes: 5 additions & 9 deletions packages/integration-tests/legacy-suite/cm/check.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(async () => {
await _dynamicInclude($includeFolder);
//await _dynamicInclude($includeFolder);
const {_highlightClick} = require('./common');


var testCase = new TestCase(1200, 1300);
Expand Down Expand Up @@ -45,15 +46,10 @@
done();
}

async function _highlightClick($element) {
await _highlight($element);
await _click($element);
}

async function loginToTrack() {
await _navigateTo('https://showroom2.cm6demo.consol.de/track/', true);
await _setValue(await _textbox('username'), 'sakulikunde');
await _setValue(await _password('password'), '4testingonly');
await _setValue(_textbox('username'), 'sakulikunde');
await _setValue(_password('password'), '4testingonly');
const btn = await _submit('Anmelden');
_highlight(btn);
await _click(btn);
Expand Down Expand Up @@ -99,7 +95,7 @@
_eval("jQuery(_textarea('/froala_editor/',_in(_div('"+$targetFieldRegExp+"')))).froalaEditor('events.trigger', 'blur');");
}
*/
_click(_div('fr-element fr-view'));
await _click(_div('fr-element fr-view'));
_eval(`
const textarea = arguments[0];
const div = arguments[1];
Expand Down
7 changes: 7 additions & 0 deletions packages/integration-tests/legacy-suite/cm/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
async function _highlightClick($element) {
await _highlight($element);
await _click($element);
}


module.exports = {_highlightClick};
20 changes: 10 additions & 10 deletions packages/integration-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"scripts": {
"demo-page": "node index",
"test": "sakuli run legacy-suite --browser=chrome",
"test": "sakuli run wetter-light --browser=firefox",
"test:debug": "node --inspect node_modules/@sakuli/cli/dist/index run legacy-suite"
},
"dependencies": {
Expand All @@ -22,7 +22,7 @@
"concurrently": "^4.1.0",
"execa": "^1.0.0",
"express": "^4.16.4",
"geckodriver": "^1.14.1",
"geckodriver": "^1.15.1",
"parcel-bundler": "^1.11.0",
"selenium-webdriver": "^4.0.0-alpha.1"
},
Expand Down
12 changes: 0 additions & 12 deletions packages/sakuli-core/src/sakuli.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {ifPresent, Maybe, throwIfAbsent} from "@sakuli/commons";
import {Project} from "./loader";
import {SakuliExecutionContextProvider, TestExecutionContext} from "./runner/test-execution-context";
import {CommandModule} from "yargs";
import * as winston from "winston";
import {SimpleLogger} from "@sakuli/commons";

let sakuliInstance: Maybe<SakuliClass>;
Expand All @@ -23,11 +22,6 @@ export function Sakuli(presetProvider: SakuliPresetProvider[] = []): SakuliInsta
export class SakuliClass {

private presetRegistry = new SakuliPresetRegistry();
// readonly logger = winston.createLogger({
// transports: [
// new winston.transports.File({filename: join(cwd(), '_logs/sakuli.log') })
// ]
// });
readonly logger = new SimpleLogger();
readonly testExecutionContext = new TestExecutionContext(this.logger);

Expand All @@ -46,12 +40,6 @@ export class SakuliClass {
get forwarder() {
return [
...this.presetRegistry.forwarders,
// <Forwarder>{
// forward(ctx: TestExecutionContext, p: Project) {
// console.log(inspect(ctx.toJson(), true, null, true));
// return Promise.resolve();
// }
// }
]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {LegacyProject} from "../loader/legacy-project.class";
import {Builder, Capabilities, ThenableWebDriver} from 'selenium-webdriver';
import {ifPresent, Maybe, throwIfAbsent} from "@sakuli/commons";
import {ifPresent, isPresent, Maybe, throwIfAbsent} from "@sakuli/commons";
import {createTestCaseClass} from "./common/test-case.class";
import {Application} from "./common/application.class";
import {Key} from "./common/key.class";
import {Environment} from "./common/environment.class";
import {sahiApi} from "./sahi/api";
import {Project, TestExecutionContext, TestExecutionLifecycleHooks} from "@sakuli/core";
import {TestFile} from "@sakuli/core/dist/loader/model/test-file.interface";
import {parse, sep} from "path";
import {isAbsolute, join, parse, sep} from "path";
import {createLoggerClass} from "./common/logger.class";

export class LegacyLifecycleHooks implements TestExecutionLifecycleHooks {
Expand Down Expand Up @@ -58,8 +58,12 @@ export class LegacyLifecycleHooks implements TestExecutionLifecycleHooks {
});
}

private currentFile: string = '';
private currentProject: Maybe<LegacyProject>;

beforeRunFile(file: TestFile, project: LegacyProject, ctx: TestExecutionContext): void {
this.currentFile = file.path;
this.currentProject = project;
}

afterRunFile(file: TestFile, project: LegacyProject, ctx: TestExecutionContext): void {
Expand All @@ -78,6 +82,18 @@ export class LegacyLifecycleHooks implements TestExecutionLifecycleHooks {
Error('Driver could not be initialized before creating sahi-api-context'));
const sahi = sahiApi(driver, ctx);
return ({
driver,
require: (path: string) => {
if (path.startsWith('./') && this.currentFile && isPresent(this.currentProject)) {
const {dir} = parse(this.currentFile);
return isAbsolute(dir)
? require(join(this.currentProject.rootDir, dir, path))
: require(join(process.cwd(), this.currentProject.rootDir, dir, path))
} else {
return require(path);
}
},
context: ctx,
TestCase: createTestCaseClass(ctx),
Application,
Key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ describe('AccessorUtil', () => {
`));
const api = createApi(driver);
const divs = await api.findElements(By.css('div'));
//expect(divs.length).toBe(2);
return expect(Promise.all(divs.map(e => e.getAttribute('id')))).resolves.toEqual([
'visibility-hidden',
'display-none',
Expand All @@ -78,4 +77,33 @@ describe('AccessorUtil', () => {
return expect(div.getAttribute('id')).resolves.toBe('div-2');
});

it('should reduce elements list by sahi class', async () => {
const {driver} = await env.getEnv();
const api = createApi(driver);
await driver.get(mockHtml(`
<div></div>
<div class="test-class"></div>
<div class="cls-1 test-class cls-2"></div>
`));
const allDivs = await driver.findElements(By.css('div'));
const divWithTestClass = await api.getElementBySahiClassName(allDivs, {className: 'test-class'});
return expect(divWithTestClass.length).toBe(2);
});

it('should find by classname', async () => {
const {driver} = await env.getEnv();
const api = createApi(driver);
await driver.get(mockHtml(`
<div>D1</div>
<div class="test-class">D2</div>
<div class="cls-1 test-class cls-2">D3</div>
`));
const divWithTestClass = await api.fetchElement({
locator: By.css('div'),
identifier: {className: 'test-class'},
relations: []
});
return expect(divWithTestClass.getText()).resolves.toBe("D2");
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ export class AccessorUtil {
}
}

private allValuesInArray(values: string[], hayStack: string[]): boolean {
const matches: WebElement[] = [];
return values.every(v => hayStack.includes(v));
}

async getElementBySahiClassName(elements: WebElement[], {className}: AccessorIdentifierAttributesWithClassName) {
const matches: WebElement[] = [];
for (let element of elements) {
const elementClasses = ((await element.getAttribute("class")) || "").split(" ");
const identifierClasses = className.split(" ");
if (this.arrayValuesAreEqual(elementClasses, identifierClasses)) {
if (this.allValuesInArray(identifierClasses, elementClasses)) {
matches.push(element)
}
}
Expand Down Expand Up @@ -105,7 +110,7 @@ export class AccessorUtil {
private async resolveByIdentifier(elements: WebElement[], identifier: AccessorIdentifier): Promise<WebElement[]> {

if (isAccessorIdentifierAttributesWithClassName(identifier)) {
elements = await this.getElementBySahiClassName(elements, identifier);
return await this.getElementBySahiClassName(elements, identifier);
}
if (isAccessorIdentifierAttributesWithSahiIndex(identifier)) {
return Promise.resolve([this.getElementBySahiIndex(elements, identifier)]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {createTestEnv, mockHtml, TestEnvironment, createTestExecutionContextMock} from "../__mocks__";
import {By, ThenableWebDriver} from "selenium-webdriver";
import {ActionApiFunction, actionApi} from "./action-api.function";
import {createTestEnv, createTestExecutionContextMock, TestEnvironment} from "../__mocks__";
import {ThenableWebDriver} from "selenium-webdriver";
import {actionApi, ActionApiFunction} from "./action-api.function";
import {AccessorUtil} from "../accessor";
import {RelationsResolver} from "../relations";
import DoneCallback = jest.DoneCallback;

jest.setTimeout(15_000);
xdescribe('ActionApiFunction', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {stripIndents} from "common-tags";
import {mouseActionApi} from "./mouse-actions-api.function";
import {keyboardActionApi} from "./keyboard-actions.function";
import {focusActionApi} from "./focus-actions.function";
import {alertActionApi} from "./alert-action.function";

export type ActionApiFunction = ReturnType<typeof actionApi>;

Expand Down Expand Up @@ -73,8 +74,13 @@ export function actionApi(
});
}

async function _navigateTo(url: string, forceReload: boolean = false): Promise<any> {
await webDriver.get(url);
async function _navigateTo(target: string, forceReload: boolean = false, credentials?: {user: string, password: string}): Promise<any> {
const url = new URL(target);
if(credentials) {
url.username = credentials.user;
url.password = credentials.password;
}
await webDriver.get(url.href);
if (forceReload) {
await webDriver.navigate().refresh()
}
Expand All @@ -94,6 +100,10 @@ export function actionApi(
await webDriver.switchTo().window(defaultWindowHandle);
}

const {
_authenticate
} = alertActionApi(webDriver, accessorUtil, ctx);

const {
_blur,
_focus,
Expand Down Expand Up @@ -148,6 +158,8 @@ export function actionApi(
_highlight: runAsAction('highlight', _highlight),
_navigateTo: runAsAction('navigateTo', _navigateTo),
_rteWrite: runAsAction('rteWrite', _rteWrite),
_eval: runAsAction('eval', _eval)
_eval: runAsAction('eval', _eval),

_authenticate: runAsAction('authenticate', _authenticate)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {ThenableWebDriver, until} from "selenium-webdriver";
import {AccessorUtil} from "../accessor";
import {TestExecutionContext} from "@sakuli/core";

export function alertActionApi(
driver: ThenableWebDriver,
accessorUtil: AccessorUtil,
ctx: TestExecutionContext
) {

async function _authenticate(user: string, password: string) {
/*
const handle = await driver.getWindowHandle();
const a = await driver.wait(until.alertIsPresent(), 5_000, 'Alert alert');
//const alert = await driver.switchTo().alert();
await a.authenticateAs(user, password);
//await driver.switchTo().window(handle);
*/
const currentUrl = await driver.getCurrentUrl();
const url = new URL(currentUrl);
url.username = user;
url.password = password;
return driver.get(url.href);
}

return ({
_authenticate
})

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {AccessorUtil} from "../accessor";
import {RelationsResolver} from "../relations";
import {focusActionApi} from "./focus-actions.function";

jest.setTimeout(15_000);
describe('focus-api', () => {
let env: TestEnvironment;
let driver: ThenableWebDriver;
Expand Down Expand Up @@ -49,7 +50,6 @@ describe('focus-api', () => {
<input type="text" id="text-input" autofocus />
`));
const inputLocator = By.css("#text-input");
By
await _blur({
locator: inputLocator,
identifier: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {By} from "selenium-webdriver";
import {isSibling} from "./is-sibling.function";
import {createTestEnv, mockHtml, TestEnvironment} from "../__mocks__";

jest.setTimeout(15_000);
describe('isSibling', () => {

let env: TestEnvironment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {TestExecutionContext} from "@sakuli/core";
import {RelationsResolver} from "./relations-resolver.class";
import {createTestEnv, mockHtml, TestEnvironment} from "../__mocks__";

describe('AccessorUtil', () => {
jest.setTimeout(15_000);
describe('RelationResolver', () => {
const testExecutionContext = mockPartial<TestExecutionContext>({});

let env: TestEnvironment;
Expand Down

0 comments on commit dae0da2

Please sign in to comment.