Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CY]Ensure requests are intercepted before the page loads #919

Merged
merged 10 commits into from
Aug 28, 2023
1 change: 1 addition & 0 deletions cypress/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ export const MenuNav = {
storageClass: ['Storage Classes', 'harvester/c/local/harvesterhci.io.storage', 'Storage Classes', ['Advanced']],
pciDevice: ['PCI Devices', 'harvester/c/local/devices.harvesterhci.io.pcidevice', 'PCI Devices', ['Advanced']],
addon: ['Addons', 'harvester/c/local/harvesterhci.io.addon', 'Addons', ['Advanced']],
secrets: ['Secrets', 'harvester/c/local/harvesterhci.io.secret', 'Secrets', ['Advanced']],
setting: ['Settings', 'harvester/c/local/harvesterhci.io.setting', 'Settings', ['Advanced']]
}
17 changes: 10 additions & 7 deletions cypress/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import { defineConfig } from 'cypress'

export default defineConfig({
// https://docs.cypress.io/guides/references/configuration#Folders-Files
defaultCommandTimeout: 240000,
pageLoadTimeout: 120000,
fixturesFolder: './fixtures',
responseTimeout: 240000,
requestTimeout: 240000,
viewportWidth: 1920,
viewportHeight: 1080,

fixturesFolder: './fixtures',
numTestsKeptInMemory: 1,
defaultCommandTimeout: 240000,
requestTimeout: 240000,
responseTimeout: 240000,
chromeWebSecurity: false,
screenshotsFolder: 'cypress/results/mochawesome-report/assets',
downloadsFolder: 'cypress/downloads',
env: {
NODE_ENV: 'production',
username: 'admin',
password: 'password1234',
mockPassword: "mockPassword",
baseUrl: 'https://online-server',
host: {
name: '',
host: [{
name: 'harvester-node-1',
disks: [
{
name: '',
devPath: '',
},
],
},
}],
image: {
name: 'openSUSE-Leap-15.3-3-NET-x86_64.qcow2',
url: 'https://download.opensuse.org/pub/opensuse/distribution/leap/15.3/appliances/openSUSE-Leap-15.3-JeOS.x86_64-15.3-OpenStack-Cloud-Current.qcow2',
Expand Down
15 changes: 15 additions & 0 deletions cypress/pageobjects/hosts.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ export class HostsPage extends CruResourcePo {
this.consoleUrl().input(value.consoleUrl)
}

checkBasicValue(name: string, options: {
customName?: string,
consoleUrl?: string,
}) {
this.goToEdit(name);

if (options?.customName) {
cy.get('.labeled-input').contains('Custom Name').next().should('have.value', options.customName);
}

if (options?.consoleUrl) {
cy.get('.labeled-input').contains('Console URL').next().should('have.value', options.consoleUrl);
}
}

enableMaintenance(name:string) {
cy.intercept('POST', `/v1/harvester/${this.realType}s/${name}?action=enableMaintenanceMode`).as('enable');
this.clickAction(name, 'Enable Maintenance Mode');
Expand Down
1 change: 1 addition & 0 deletions cypress/pageobjects/image.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class ImagePage extends CruResourcePo {
this.search(vmName);
cy.wait(2000);
cy.wrap('async').then(() => {
this.table.clickFlatListBtn();
this.table.find(vmName, 7, namespace, 4).then((index: any) => {
if (typeof index === 'number') {
cy.get(`[data-testid="sortable-table-${index}-row"]`).find('td').eq(2).invoke('text').then((volumeName) => {
Expand Down
30 changes: 19 additions & 11 deletions cypress/pageobjects/rancher.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { List } from 'cypress/types/lodash';

const constants = new Constants();
const settings = new SettingsPagePo();
var registrationURL;

interface ValueInterface {
namespace?: string,
Expand All @@ -34,7 +33,7 @@ export class rancherPage {
private main_page_title = '.title';
private dashboardURL = 'dashboard/home';

private boostrap_page_welcome = '.info-box > :nth-child(1)';
private boostrap_page_welcome = '[data-testid="first-login-message"]';
private boostrap_page_boostrapPWInput = 'input';
private boostrap_page_boostrapPWSubmit = '#submit > span';
private boostrap_page_radioSelectPW = ':nth-child(2) > .radio-container';
Expand Down Expand Up @@ -102,6 +101,23 @@ export class rancherPage {

// }

/**
* To check whether the Harvester is first time to login.
* @returns the boolean value to identify is first time login or not.
*/
public static isFirstTimeLogin(): Promise<boolean> {
return new Promise((resolve, reject) => {
cy.intercept('GET', '/v1/management.cattle.io.setting?exclude=metadata.managedFields').as('getFirstLogin')
.visit("/")
.wait('@getFirstLogin').then(login => {
const data: any[] = login.response?.body.data;
const firstLogin = data.find(v => v?.id === "first-login");
resolve(firstLogin.value === 'true');
})
.end();
});
}

/**
* First time login using ssh
*/
Expand Down Expand Up @@ -140,15 +156,7 @@ export class rancherPage {

cy.visit('/')
cy.wait(1000).get('body').then($body => {
if ($body.find(this.boostrap_page_welcome).length) {
cy.log('First time login')
this.firstTimeLogin();

} else {
cy.log('Not first time login')
this.login();
}

this.login();
})

this.validateLogin()
Expand Down
8 changes: 5 additions & 3 deletions cypress/pageobjects/settings.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ export default class SettingsPagePo extends CruResource {
/**
* Go to the setting edit page. Then it checks the URL
*/
clickMenu(name: string, actionText: string, urlSuffix: string, type?: string) {
clickMenu(name: string, actionText: string, urlSuffix: string, type?: string, tab?: string) {
const editPageUrl = type ? `/harvester/c/local/${type}` : constants.settingsUrl;
if (tab === 'UI') {
cy.get('[data-testid="btn-ui"]').contains('UI').click();
}

cy.get(`.advanced-setting #${name} button`).click()

Expand Down Expand Up @@ -60,9 +63,8 @@ export default class SettingsPagePo extends CruResource {
checkUiSource(type: string, address: string) {
new LabeledSelectPo('section .labeled-select.hoverable', `:contains("Value")`).select({option: type})
this.update('ui-source');
this.checkIsCurrentPage();
this.checkIsCurrentPage(false);
cy.reload();

cy.intercept('GET', address).as('fetch');
cy.wait('@fetch', { timeout: constants.timeout.maxTimeout}).then(res => {
cy.log(`Check Passed ui-source (${type})`)
Expand Down
14 changes: 13 additions & 1 deletion cypress/pageobjects/virtualmachine.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class VmsPage extends CruResourcePo {
this.clickAction(name, 'Take VM Snapshot');
cy.get('.v--modal-box .card-title').find('h4').contains('Take VM Snapshot');

new LabeledInputPo('.v--modal-box .labeled-input', `:contains("Name")`).input(snapshotName)
new LabeledInputPo('.v--modal-box .labeled-input', `:contains("Name *")`).input(snapshotName)
cy.get('.v--modal-box button').contains('Create').click();
cy.get('.growl-container .growl-list').find('.growl-text div').contains('Succeed');
}
Expand Down Expand Up @@ -220,6 +220,18 @@ export class VmsPage extends CruResourcePo {
this.deleteFromStore(id, HCI.VMI); // You need to wait for the vmi to be deleted as well, because it will not be deleted until the vm is deleted
}

deleteVMFromUI(namespace: string, name: string) {
this.goToList();
this.clickAction(name, 'Delete');
cy.get('[data-testid="card"]').get('[type="checkbox"]').each(($checkbox) => {
if (!$checkbox.is(':checked')) {
$checkbox.click();
}
});

cy.get('.v--modal-box button').contains('Delete').click();
}

network() {
return new LabeledSelectPo('section .labeled-select.hoverable', `:contains("Network")`)
}
Expand Down
9 changes: 4 additions & 5 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const constants = new Constants();
require('cy-verify-downloads').addCustomCommand();

Cypress.Commands.add('login', (params = {}) => {
const url = params.url || constants.dashboardUrl;
let url = params.url || constants.dashboardUrl;
const username = params.username || Cypress.env('username');
const password = params.password || Cypress.env('password');

const isDev = Cypress.env('NODE_ENV') === 'dev';
const baseUrl = isDev ? Cypress.config('baseUrl') : `${Cypress.config('baseUrl')}/dashboard`;
cy.visit(`/auth/login`);
cy.intercept('GET', '/v3-public/authProviders').as('authProviders');
cy.visit(`/auth/login`);
cy.wait('@authProviders').then(res => {
const { CSRF } = cookie.parse(document.cookie);
cy.request({
Expand All @@ -33,9 +33,8 @@ Cypress.Commands.add('login', (params = {}) => {
'x-api-csrf': CSRF
}
}).then(() => {
cy.visit(url); // After successful login, you can switch to the specified page, which is the home page by default
cy.get('.initial-load-spinner', { timeout: constants.timeout.maxTimeout })
cy.get(".dashboard-content .product-name").contains("Harvester")
cy.visit(url).log(url); // After successful login, you can switch to the specified page, which is the home page by default
cy.get(".dashboard-content .product-name", { timeout: constants.timeout.maxTimeout }).contains("Harvester")
});
})
});
Expand Down
3 changes: 2 additions & 1 deletion cypress/testcases/backupAndSnapshot/vmSnapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PageUrl } from "@/constants/constants";
const vms = new VmsPage();
const vmsnapshots = new VMSnapshot();

describe('VM Form Validation', () => {
describe('VM snapshot Form Validation', () => {
const vmName = 'test';
let createVMSnapshotSuccess: boolean = false ;
const vmSnapshotName = 'test-vm-snapshot';
Expand All @@ -25,6 +25,7 @@ describe('VM Form Validation', () => {
buttonText: 'Add Volume',
create: false,
image: `default/${Cypress._.toLower(imageEnv.name)}`,
size: 4
}];

vmsnapshots.deleteFromStore(`${namespace}/${vmSnapshotName}`)
Expand Down
2 changes: 1 addition & 1 deletion cypress/testcases/backupAndSnapshot/volumeSnapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Validation volume snapshot', () => {
// create a new volume
volumes.goToCreate();
volumes.setNameNsDescription(volumeName, namespace);
volumes.setBasics({size: '10'});
volumes.setBasics({size: '4'});
volumes.save();
volumes.censorInColumn(volumeName, 3, namespace, 4, 'Ready');

Expand Down
22 changes: 8 additions & 14 deletions cypress/testcases/dashboard/hosts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { HostsPage } from "@/pageobjects/hosts.po";
import { EditYamlPage } from "@/pageobjects/editYaml.po";
import { LoginPage } from "@/pageobjects/login.po";
import { HCI } from '@/constants/types'

const hosts = new HostsPage();
const editYaml = new EditYamlPage();
const login = new LoginPage();

/**
* TODO:
* This will insert custom YAML into the hosts page while editing
*/
export function insertCustomYAML() {}
describe('should insert custom name into YAML', () => {
it.skip('should insert custom name into YAML', () => {
cy.login();
Expand All @@ -20,36 +18,32 @@ describe('should insert custom name into YAML', () => {
});
});

export function CheckEdit() {}
describe('Check edit host', () => {
it('Check edit host', () => {
it.only('Check edit host', () => {
cy.login();

const host = Cypress.env('host')[0];

cy.visit(`/harvester/c/local/${HCI.HOST}/${host.name}?mode=edit`)

const customName = 'test-custom-name'
const consoleUrl = 'test-console-url'

hosts.goToEdit(host.name);
hosts.setValue({
customName,
consoleUrl,
})

cy.intercept('PUT', `/v1/harvester/nodes/*`).as('update');

// TODO: The footer position is inconsistent on the host edit page.
hosts.update(host.name)

cy.wait('@update').then(res => {
const annotations = res.response?.body?.metadata?.annotations || {}
expect(annotations['harvesterhci.io/host-custom-name'], 'Check custom name').to.equal(customName);
expect(annotations['harvesterhci.io/host-console-url'], 'Check console url').to.equal(consoleUrl);
hosts.goToEdit(customName);
hosts.checkBasicValue(customName, {
customName,
consoleUrl
})
})
})

export function CheckAddDisk() {}
describe('Check Add disk', () => {
it.skip('Check Add disk', () => {
cy.login();
Expand Down
3 changes: 2 additions & 1 deletion cypress/testcases/dashboard/nav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ describe("UI url", () => {
})
})

// https://harvester.github.io/tests/manual/ui/verify-url/
it("Verify the Harvester icon on the left top corner", () => {
cy.get('.dashboard-content header .menu-spacer').find('img', {timeout: 4000}).then(($el) => {
cy.get('.dashboard-content header .menu-spacer').find('img').then(($el) => {
const src = $el.attr('src');
expect(src).to.match(/harvester.*\.svg/);
})
Expand Down
4 changes: 1 addition & 3 deletions cypress/testcases/dashboard/support.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SupportPage } from "@/pageobjects/support.po";
import {Constants} from "../../constants/constants";
import { Constants } from "@/constants/constants";
const constants = new Constants;

describe("Support Page", () => {
Expand Down Expand Up @@ -30,13 +30,11 @@ describe("Support Page", () => {
it("Should be Downloaded", () => {
const kubeconfig = `${Cypress.config("downloadsFolder")}/local.yaml`
page.downloadKubeConfigBtn.click()

cy.readFile(kubeconfig)
.should("exist")

cy.task("readYaml", kubeconfig)
.should(val => expect(val).to.not.be.a('string'))

})
})

Expand Down
6 changes: 3 additions & 3 deletions cypress/testcases/image/images.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('Delete VM with exported image', () => {
// create VM
vms.goToCreate();
vms.setNameNsDescription(VM_NAME, namespace);
vms.setBasics('2', '4');
vms.setBasics('1', '1');
vms.setVolumes(volumes);
vms.save();

Expand Down Expand Up @@ -215,7 +215,7 @@ describe('Update image labels after deleting source VM', () => {
// create VM
vms.goToCreate();
vms.setNameNsDescription(VM_NAME, namespace);
vms.setBasics('2', '4');
vms.setBasics('1', '1');
vms.setVolumes(volumes);
vms.save();
vms.checkState({name: VM_NAME, namespace});
Expand Down Expand Up @@ -335,7 +335,7 @@ describe('Create a ISO image via upload', () => {

vms.goToCreate();
vms.setNameNsDescription(VM_NAME, namespace);
vms.setBasics('2', '4');
vms.setBasics('1', '1');
vms.setVolumes(volumes);
vms.save();
vms.checkState({name: VM_NAME});
Expand Down
Loading
Loading