/*********************************************************************
 * Copyright (c) 2019 Red Hat, Inc.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 **********************************************************************/ import 'reflect-metadata' ; import { injectable , inject } from 'inversify' ; import { CLASSES } from '../../../inversify.types' ; import { DriverHelper } from '../../../utils/DriverHelper' ; import { Logger } from '../../../utils/Logger' ; import { By , error } from 'selenium-webdriver' ; import { TimeoutConstants } from '../../../TimeoutConstants' ; import { LeftToolBar } from '../LeftToolBar' ; import { TestConstants } from '../../../TestConstants' ; import { TopMenu } from '../TopMenu' ; import { QuickOpenContainer } from '../QuickOpenContainer' ; @ injectable ( ) export class GitHubPullRequestPlugin { constructor ( @ inject ( CLASSES . DriverHelper ) private readonly driverHelper , @ inject ( CLASSES . LeftToolBar ) private readonly leftToolbar , @ inject ( CLASSES . TopMenu ) private readonly topMenu , @ inject ( CLASSES . QuickOpenContainer ) private readonly quickOpenContainer ) { } async openView ( timeout = TimeoutConstants . TS_PROJECT_TREE_TIMEOUT ) { Logger . debug ( `GithubPullRequestPlugin.openView` ) ; await this . leftToolbar . selectView ( 'GitHub' , timeout ) ; } async waitViewIcon ( timeout = TimeoutConstants . TS_PROJECT_TREE_TIMEOUT ) { Logger . debug ( `GithubPullRequestPlugin.waitViewIcon` ) ; await this . leftToolbar . waitToolIcon ( 'GitHub' , timeout ) ; } async clickTreeItem ( itemTitle , timeout = TimeoutConstants . TS_COMMON_PLUGIN_TEST_TIMEOUT ) { Logger . debug ( `GithubPullRequestPlugin.clickTreeItem` ) ; await this . driverHelper . waitAndClick ( this . getTreeItemLocator ( itemTitle ) , timeout ) ; } async waitTreeItem ( itemTitle , timeout = TimeoutConstants . TS_COMMON_PLUGIN_TEST_TIMEOUT ) { Logger . debug ( `GithubPullRequestPlugin.waitTreeItem` ) ; await this . driverHelper . waitVisibility ( this . getTreeItemLocator ( itemTitle ) , timeout ) ; } async waitTreeItemExpanded ( itemTitle , timeout = TimeoutConstants . TS_COMMON_PLUGIN_TEST_TIMEOUT ) { Logger . debug ( `GithubPullRequestPlugin.waitTreeItemExpanded` ) ; await this . driverHelper . waitVisibility ( this . getExpandedTreeItemIconLocator ( itemTitle ) , timeout ) ; } async waitTreeItemCollapsed ( itemTitle , timeout = TimeoutConstants . TS_COMMON_PLUGIN_TEST_TIMEOUT ) { Logger . debug ( `GithubPullRequestPlugin.waitTreeItemCollapsed` ) ; await this . driverHelper . waitVisibility ( this . getCollapsedTreeItemIconLocator ( itemTitle ) , timeout ) ; } async expandTreeItem ( itemTitle , timeout = TimeoutConstants . TS_COMMON_PLUGIN_TEST_TIMEOUT ) { Logger . debug ( `GithubPullRequestPlugin.expandTreeItem` ) ; await this . driverHelper . getDriver ( ) . wait ( async ( ) => { if ( await this . isTreeItemCollapsed ( itemTitle ) ) { await this . clickTreeItem ( itemTitle , timeout ) ; } try { await this . waitTreeItemExpanded ( itemTitle , timeout / 4 ) ; return true ; } catch ( err ) { if ( ! ( err instanceof error . TimeoutError ) ) { throw err ; } Logger . info ( `The '${ itemTitle }' item has not been expanded, try again` ) ; await this . driverHelper . wait ( TestConstants . TS_SELENIUM_DEFAULT_POLLING ) ; } } , timeout ) ; } async clickSignInButton ( timeout = TimeoutConstants . TS_COMMON_PLUGIN_TEST_TIMEOUT ) { Logger . debug ( `GithubPullRequestPlugin.clickSignInButton` ) ; const signInButtonLocator = By . xpath ( `//div[@id='pr:github']//button[text()='Sign in']` ) ; await this . driverHelper . waitAndClick ( signInButtonLocator , timeout ) ; } async createPrFromCommandMenu ( ) { Logger . debug ( `GithubPullRequestPlugin.createPrFromCommandMenu` ) ; await this . topMenu . selectOption ( 'View' , 'Find Command...' ) ; await this . quickOpenContainer . typeAndSelectSuggestion ( 'pull' , 'GitHub Pull Requests: Create Pull Request' ) ; } private async isTreeItemCollapsed ( itemTitle ) { return await this . driverHelper . isVisible ( this . getCollapsedTreeItemIconLocator ( itemTitle ) ) ; } private getTreeItemXpath ( itemTitle ) { return `//div[@id='plugin-view-container:github-pull-requests']//div[@title='${ itemTitle }']` ; } private getTreeItemLocator ( itemTitle ) { return By . xpath ( this . getTreeItemXpath ( itemTitle ) ) ; } private getCollapsedTreeItemIconLocator ( itemTitle ) { const collapsedTreeItemIconXpath = `${ this . getTreeItemXpath ( itemTitle ) }` + `/parent::div//div[@data-node-id and contains(@class, 'theia-mod-collapsed')]` ; return By . xpath ( collapsedTreeItemIconXpath ) ; } private getExpandedTreeItemIconLocator ( itemTitle ) { const expandedTreeItemIconXpath = `${ this . getTreeItemXpath ( itemTitle ) }` + `/parent::div//div[@data-node-id and not(contains(@class, 'theia-mod-collapsed'))]` ; return By . xpath ( expandedTreeItemIconXpath ) ; } }