Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
11 kB
4
Indexable
Never
package com.workplace.reservation.test.frontend.it;

import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.testng.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.Keys;
import java.net.URI;
import java.net.URISyntaxException;
import com.glide.script.GlideRecord;


import java.util.List;
import com.snc.glide.it.annotations.WithScopedApp;
import com.snc.glide.it.rules.DataLoader;
import com.snc.glide.it.rules.RecordCleaner;
import com.snc.glide.it.runners.Step;
import com.snc.sdlc.annotations.Story;
import com.snc.sdlc.annotations.TestCase;
import com.snc.selenium.core.SNCTest;
import com.snc.selenium.runner.GlideUiRunner;
import com.snc.selenium.runner.WithUser;
import com.workplace.reservation.test.objects.WSDSearchReservableObject;
import com.workplace.reservation.test.objects.WSDGeneralWidgetObjects;
import com.workplace.reservation.test.objects.WSDReservationWidgetObjects;

@Story({ "STRY54825899", "STRY54826164" })
//@WithScopedApp(value = { "app-wsd-core", "app-wsd-reservation" }, loadDemoData = false, nodeRestart = false)
@RunWith(GlideUiRunner.class)

public class WSDOverlappingReservationIT extends SNCTest implements WSDSearchReservableObject, WSDGeneralWidgetObjects, WSDReservationWidgetObjects{
	
	WebDriver driver = getDriver();
	@ClassRule	
	public static final DataLoader testData = new DataLoader();
	public static final RecordCleaner recordCleaner = new RecordCleaner();
	
	@BeforeClass
	public void loadData() {
		testData.loadResource("sys_user.xml");
		testData.loadResource("sys_user_has_role.xml");
		testData.loadResource("sn_wsd_rsv_m2m_reservable_module_view_configuration.xml");
		testData.loadResource("sn_wsd_rsv_reservable_module.xml");
		testData.loadResource("sn_wsd_core_workplace_location.xml");
		testData.loadResource("sn_wsd_rsv_reservable_view.xml");
		testData.loadResource("sn_wsd_rsv_reservation.xml");
	}
	
	@AfterClass
	public void deleteRecord() {
		recordCleaner.clean();
		testData.unloadAll();
	}
	
	@Story({"STRY54825899", "STRY54826164", "STRY54905741", "STRY54826169", "STRY54984211", "STRY54960124"})
	@TestCase({ "TNTC0266326", "TNTC0267458", "TNTC0269493", "TNTC0272113", "TNTC0272864", "TNTC0277111" })
	@WithUser("rsv_user_1")
	@Step(value = 1, info = "Checking for overlapping reservations")
	public void verifyOverlappingRsv() {
	
		searchForSpaceInSearchPage();
		landOnRsvPageFromSearchPage();
		createRsv();


		searchForSpaceInSearchPage();
		//verify overlapping msg in search page
		verifyWarningMsg(overlappingMsg);
		landOnRsvPageFromSearchPage();
		//verify overlapping msg in intermediate page
		verifyWarningMsg(overlappingMsg);
		createRsv();		
				
		WebElement actionsButton = item.findElement(By.className(ACTIONS_BUTTON_CLASS_NAME));
		actionsButton.click();
		
		WebElement link = item.findElement(By.linkText(CHANGE_RSV_DETAILS));
		link.click();
		
		//checks overlapping reservation for private reservation where opened by is logged in user 
		WebElement onBehalfOf = item.findElement(By.id(RSV_ON_BEHALF_OF_ID));
		onBehalfOf.click();
		onBehalfOf.sendKeys(RSV_USER_2);
		WebElement firstOptionInOnBehalfOf = item.findElement(By.className(SELECT2_HIGHLIGHTED));
		firstOptionInOnBehalfOf.click();
		verifyWarningMsg(overlappingMsgForDiffOnbehalfOfUser);
		
		//checks overlapping reservation for private reservation where opened by is not logged in user 
		onBehalfOf.click();
		onBehalfOf.sendKeys(RSV_USER_3);
		WebElement firstOptionInOnBehalfOfPicker = item.findElement(By.className(SELECT2_HIGHLIGHTED));
		firstOptionInOnBehalfOfPicker.click();
		//As this is private reservation, it shouldn't be visible to loggedin user
		verifyNoWarningMsgForPrivateRsv();
		
		WebElement dateLocationButton = item.findElement(By.className(CHANGE_DATE_OR_LOCATION_CLASS_NAME));
		dateLocationButton.click();
		
		//verify overlapping msg in edit scenario in search page
		verifyWarningMsg(overlappingMsg);
		
	}
	
	
	/*
	@Story({"STRY54960124"})
	@TestCase({"TNTC0277111" })
	@WithUser("shared_rsv_user")
	@Step(value = 2, info = "Checking for overlapping private reservations visible for notification recipients")
	public void verifyOverlappingPrivateRsvForSharedUser() {
		//page.load("/ws?id=wsd_reservation&reservable_ids=0ad6b7f9c3111010cc7060bf4b40dd30&start=2026-01-02T08:00:00Z&end=2026-01-02T09:00:00Z&reservable_module=5db44502dbb650106c731dcd13961937", true);
		
		searchForSpaceInSearchPage();
		landOnRsvPageFromSearchPage();
		//checks overlapping reservation for private reservation where opened by is notification recipient
		WebElement onBehalfOf = item.findElement(By.id(RSV_ON_BEHALF_OF_ID));
		onBehalfOf.click();
		onBehalfOf.sendKeys(RSV_USER_3);
		WebElement firstOptionInOnBehalfOf = item.findElement(By.className(SELECT2_HIGHLIGHTED));
		firstOptionInOnBehalfOf.click();
		verifyWarningMsg(overlappingMsgForDiffOnbehalfOfUserRsv3);
		
	}*/
	/*
	
	@Story({"STRY54960124"})
	@TestCase({"TNTC0277111" })
	@WithUser("rsv_attendee_user")
	@Step(value = 3, info = "Checking for overlapping reservations visible for attendees")
	public void verifyOverlappingPrivateRsvForAttendee() {
		//page.load("/ws?id=wsd_reservation&reservable_ids=0ad6b7f9c3111010cc7060bf4b40dd30&start=2026-01-02T08:00:00Z&end=2026-01-02T09:00:00Z&reservable_module=5db44502dbb650106c731dcd13961937", true)
		
		searchForSpaceInSearchPage();
		landOnRsvPageFromSearchPage();
		//checks overlapping reservation for private reservation where opened by is attendee
		WebElement onBehalfOf = item.findElement(By.id(RSV_ON_BEHALF_OF_ID));
		onBehalfOf.click();
		onBehalfOf.sendKeys(RSV_USER_3);
		WebElement firstOptionInOnBehalfOf = item.findElement(By.className(SELECT2_HIGHLIGHTED));
		firstOptionInOnBehalfOf.click();
		verifyWarningMsg(overlappingMsgForDiffOnbehalfOfUserRsv3);
		
	}
	
*/
	
	private String getRsvIdFromUrl(String actualUrl) {
		String reservationId = null;
		try {
			URI uri = new URI(actualUrl);
			String query = uri.getQuery();
			String[] queryParams = query.split("&");
			
			for (String param : queryParams) {
                String[] keyValue = param.split("=");
                if (keyValue[0].equals("reservation_id")) {
                    reservationId = keyValue[1];
                    break;
                }
            }			
		} catch (URISyntaxException e) {
            throw new RuntimeException(e.getMessage());
        }
		return reservationId;
	}
	
	public void searchForSpaceInSearchPage() {
		WebDriver driver = getDriver();
		
		page.load("/ws?id=" + WSD_RESERVABLE_SEARCH_PAGE_ID, true);
		
		WebDriverWait wait = new WebDriverWait(driver, MAX_ASYNC_TIME);
		wait.until(ExpectedConditions.urlContains(WSD_RESERVABLE_SEARCH_PAGE_ID));
		
		// Find the UI tag containing the reservableModule list
		WebElement uiTagReservableModules = item.findElement(By.className(RESERVABLE_MODULE_TYPES_LIST));
		// Find all the list items in the UI tag
		List<WebElement> reservableModulelistItems = uiTagReservableModules.findElements(By.cssSelector(LIST_TAG));

		// Iterate through the list items to find the desired one
		for (WebElement reservableModuleItem : reservableModulelistItems) {
		    if (reservableModuleItem.getText().equals(RESERVABLE_MODULE_NAME)) {
		    	reservableModuleItem.click();
		        break;
		    }
		}
		
		WebElement unifiedPicker = item.findElement(By.className(UNIFIED_PICKER_CONTAINER_CLASS_NAME));
        WebElement pickerInput = unifiedPicker.findElement(By.tagName(INPUT_TAG));
        pickerInput.sendKeys(BUILDING_PICKER_4mationHNKUtrecht_BUILDING_VALUE); // private location
        selectFirstValueInDropDown(getDriver());
		
		WebElement startDatePicker = driver.findElement(By.id(START_TIME_ID));
		startDatePicker.clear();
		startDatePicker.sendKeys(START_DATE_TIME);
		startDatePicker.sendKeys(Keys.TAB);
		
		WebElement endDatePicker = driver.findElement(By.id(END_TIME_ID));
		endDatePicker.clear();
		endDatePicker.sendKeys(END_DATE_TIME);
		endDatePicker.sendKeys(Keys.TAB);

		WebElement searchButton = item.findElement(By.className(BUTTON_CLASS_NAME));
		searchButton.click();
		searchButton.click();
	}
	
	
	public void landOnRsvPageFromSearchPage() {
		WebElement addButton = item.findElement(SelectSpace);
		//WebElement addButton = item.findElement(By.name("Add"));
		//WebElement addButton =item.findElement(By.cssSelector("[id*='add']"));
		addButton.click();
		WebElement nextButton = item.findElement(By.className(BTN_NEXT_CLASS_NAME));
		nextButton.click();
		
		//verify overlapping msg in intermediate page
		//verifyWarningMsg(overlappingMsg);
		
		WebElement submitButton = item.findElement(By.className(SUBMIT_RSV_BTN));
		submitButton.click();
		
		String actualUrl = driver.getCurrentUrl();
		String rsvId = getRsvIdFromUrl(actualUrl);
		
		if(!rsvId.isEmpty()) {
			GlideRecord rsvGr = new GlideRecord(RESERVATION_TABLE_NAME);
			if(rsvGr.get(rsvId)) 
				recordCleaner.add(rsvGr);
		}
	}
	
	
	public void createRsv() {
		WebElement submitButton = item.findElement(By.className(SUBMIT_RSV_BTN));
		submitButton.click();
		
		String actualUrl = driver.getCurrentUrl();
		String rsvId = getRsvIdFromUrl(actualUrl);
		
		if(!rsvId.isEmpty()) {
			GlideRecord rsvGr = new GlideRecord(RESERVATION_TABLE_NAME);
			if(rsvGr.get(rsvId)) 
				recordCleaner.add(rsvGr);
		}
	}
	
	
	public void verifyNoWarningMsgForPrivateRsv() {
		WebElement alertDiv = item.findElement(By.className(ALERT_WARNING_CLASS_NAME));
	    Assert.assertNull(alertDiv);
		
	}
	
	public void verifyWarningMsg(String overlappingMsgText) {
		System.out.println("start of verifyWarningMsg");
		// Find the div element
		WebElement alertDiv = item.findElement(By.className(ALERT_WARNING_CLASS_NAME));
		// Get the text within the div element
		String alertDivText = alertDiv.getText();		
		MatcherAssert.assertThat(alertDivText, CoreMatchers.containsString(overlappingMsgText));
		System.out.println("end of verifyWarningMsg");
	}
	
    public void selectFirstValueInDropDown(WebDriver driver) {
    	timers.waitUntilPageLoaded(5);
		((JavascriptExecutor) driver).executeScript(
				"$(\".select2-results-dept-0.select2-result.select2-result-selectable.select2-highlighted\").trigger(\"mousedown\").trigger(\"mouseup\")",
				"");
		timers.waitUntilPageLoaded(5);
    }

}