Java tutorial
package com.vmware.veei.qe.devcenter; import static org.assertj.core.api.Assertions.assertThat; import com.vmware.veei.qe.devcenter.authenticators.DevCenterAuthenticator; import com.vmware.veei.qe.devcenter.utils.WaitUtils; import com.vmware.veei.qe.selenium.annotations.Authenticate; import com.vmware.veei.qe.selenium.annotations.NavigateTo; import com.vmware.veei.qe.selenium.annotations.SeleniumTest; import com.vmware.veei.qe.selenium.annotations.SeleniumTestExecutionListener; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; /** * Description :[Sample Exchange] test cases * * @author Tushar (tverma@vmware.com) && Mandeep (mandeepm@vmware.com) * */ @TestExecutionListeners(SeleniumTestExecutionListener.class) @SeleniumTest @Authenticate(auth = DevCenterAuthenticator.class) public class SampleExchangeTest extends AbstractTestNGSpringContextTests { private final static Log _log = LogFactory.getLog(SampleExchangeTest.class); @Autowired private WebDriver driver; @Autowired private String baseUrl; private final static String SAMPLE_CODE_URL = "/samples"; /** * S87854: [Sample Exchange] Search and browse for code samples */ @Test(groups = { "test", "stage" }) @NavigateTo(SAMPLE_CODE_URL) public void testSampleCodeSearchBrowse() { String titleText = "stest"; // #action 1: Adding sample addSample(titleText); // #action 2: Navigate to default page. driver.get(baseUrl + SAMPLE_CODE_URL); selectTab("Samples"); int defaultSampleCodeCount = driver.findElements(By.cssSelector("#search-results-list .title-list li")) .size(); // #action 3: Filter search input check WebElement filterSearchRadioButton = driver.findElement(By.cssSelector("input[value='Python']")); filterSearchRadioButton.click(); int filterSearchCount = driver.findElements(By.cssSelector("#search-results-list .title-list li")).size(); // #expects: Search result should be less then by default result for Filter Search assertThat(defaultSampleCodeCount).isGreaterThanOrEqualTo(filterSearchCount); // #action 4: Navigate to default page. selectTab("Samples"); WebElement globalSearchInputField = driver .findElement(By.cssSelector("#main-content .internal-global-search .search input[type='text']")); // #action 5: Giving text input for search text global search text input globalSearchInputField.sendKeys(titleText); globalSearchInputField.submit(); int sampleCodeSearchCount = driver.findElements(By.cssSelector("#search-results-list .title-list li")) .size(); // #expects: Search result should be less then by default result for Global Search if (defaultSampleCodeCount > 0) { assertThat(defaultSampleCodeCount).isGreaterThanOrEqualTo(sampleCodeSearchCount); } // #action 6: Select Tab : My sample selectTab("My Samples"); // #action 7 : Selecting latest first Select dropdown = new Select(driver.findElement(By.cssSelector("#search-results-sort-by"))); dropdown.selectByIndex(2); List<WebElement> sampleCodeLinkList = driver .findElements(By.cssSelector("#search-results-list .title-list li")); if (sampleCodeSearchCount >= 1) { for (WebElement samplecodelink : sampleCodeLinkList) { String linkHeadText = samplecodelink.findElement(By.cssSelector(".title")).getText(); if (linkHeadText.equals(titleText)) { samplecodelink.click(); samplecodelink.findElement(By.cssSelector("div a")).click(); String sampleCodePageHeadText = driver.findElement(By.cssSelector(".header .span9 h1 ")) .getText(); // #expects: Validating browsed link header with headText assertThat(linkHeadText).isEqualTo(sampleCodePageHeadText); // #expects: Checking Faveroite link WebElement favoriteLink = driver.findElement(By.cssSelector(".favoriteHeart")); String favoriteCount = favoriteLink.getText(); favoriteLink.click(); assertThat(driver.findElement(By.cssSelector(".favoriteHeart"))).isNotEqualTo(favoriteCount); // #action 8: Deleting created JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("$('.sample-delete-dialog').submit();"); new WebDriverWait(driver, 10).until( ExpectedConditions.presenceOfElementLocated(By.cssSelector("#search-results-list"))); break; } } } } /** * @param searchText * */ private void addSample(String titleText) { // #action 1: Clicking on Add New sample button driver.findElement(By.cssSelector(".admin-actions a")).click(); JavascriptExecutor jse = (JavascriptExecutor) driver; WebElement radioButtonList = new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("input[value='SNIPPET']"))); radioButtonList.click(); jse.executeScript("$('textarea[name=sample-snippet-content]').val('This is a Sample Snippet');"); jse.executeScript("$('input[name=sampleTitle]').val('" + titleText + "');"); // Giving input to Ckeditor // Waiting for 10 second to load ckeditor WebElement ckeditor = new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".cke_wysiwyg_div"))); ckeditor.click(); ckeditor.sendKeys("Test Description"); // Selecting language Option Select dropdown = new Select(driver.findElement(By.name("sample-language"))); dropdown.selectByIndex(1); // Selecting License Option Select sampleLicenseDropdown = new Select(driver.findElement(By.name("sample-license"))); sampleLicenseDropdown.selectByIndex(1); // Checking multiple inputBoxes driver.findElement(By.cssSelector(".add-platform-input")).click(); List<WebElement> platformInputs = driver.findElements(By.name("sdk-tools")); List<WebElement> platformVersions = driver.findElements(By.name("sdk-tools-versions")); // #expects: Checking multiple checkboxes should have greater then one input for version nnd sdk-tool inputBox assertThat(platformInputs.size()).isGreaterThan(1); assertThat(platformVersions.size()).isGreaterThan(1); // Adding version WebElement version = driver.findElement(By.name("sdk-tools-versions")); version.sendKeys("1.0"); // Add Value to Hidden Field required to Create Sample jse.executeScript("$('[name=sdk-tools]').val('Airwatch').trigger('change');"); jse.executeScript("$('input[name=tags]').val('testing').trigger('change');"); // Agreement check driver.findElement(By.cssSelector("input[type='checkbox']")).click(); // #action 2: submitting the sample WebElement uploadButtonElement = driver.findElement(By.cssSelector("input[type=submit]")); uploadButtonElement.click(); } /** * S87854: [Sample Exchange] Search and browse for Sample Requests */ @Test(groups = { "test", "stage" }) @NavigateTo(SAMPLE_CODE_URL) public void testSampleRequestSearchBrowse() { // #action 1: Selecting request tab selectTab("Requests"); // #action 2: Adding request String titleText = "Rtest"; String language = "java"; String sdkTool = "Airwatch"; addRequest(titleText, language, sdkTool); // #action 3: Navigating to the default page after adding request driver.get(baseUrl + SAMPLE_CODE_URL); selectTab("Requests"); // Total count of sample code presented int defaultRequestCount = driver.findElements(By.cssSelector("#search-results-list .title-list li")).size(); // Filter search input check WebElement filterSearchRadioButton = driver.findElement(By.cssSelector("input[value='Python']")); filterSearchRadioButton.click(); int filterSearchCount = driver.findElements(By.cssSelector("#search-results-list .title-list li")).size(); // #expects: Search result should be less then by default result for Filter Search assertThat(defaultRequestCount).isGreaterThanOrEqualTo(filterSearchCount); // #action 4: Navigating to the default page selectTab("Requests"); WebElement globalSearchInputField = driver .findElement(By.cssSelector("#main-content .internal-global-search .search input[type='text']")); // #action 5: Giving text input for search text global search text input globalSearchInputField.sendKeys(titleText); globalSearchInputField.submit(); // #action 6: Count the total search result int requestSearchCount = driver.findElements(By.cssSelector("#search-results-list .title-list li")).size(); // #expects: Search result should be less then by default result for Global Search if (defaultRequestCount > 0) { assertThat(defaultRequestCount).isGreaterThanOrEqualTo(requestSearchCount); } // #action 7: Select Tab : My request selectTab("My Requests"); // #action 8 : Selecting latest first Select dropdown = new Select(driver.findElement(By.cssSelector("#search-results-sort-by"))); dropdown.selectByIndex(2); List<WebElement> requestLinkList = driver .findElements(By.cssSelector("#search-results-list .title-list li")); if (requestSearchCount >= 1) { for (WebElement requestlink : requestLinkList) { String linkHeadText = requestlink.findElement(By.cssSelector("div a")).getText(); if (linkHeadText.equals(titleText)) { requestlink.click(); requestlink.findElement(By.cssSelector("div a")).click(); String requestlinkPageHeadText = driver.findElement(By.cssSelector(".header-title h1 ")) .getText(); // #expects: Validating browsed link header assertThat(linkHeadText).isEqualTo(requestlinkPageHeadText); // #action 9: Deleting created JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("$('.request-delete-dialog').submit();"); new WebDriverWait(driver, 10).until( ExpectedConditions.presenceOfElementLocated(By.cssSelector("#search-results-list"))); break; } } } } /** * @param sdkTool * @param language * @param searchText */ private void addRequest(String titleText, String language, String sdkTool) { // #action 1: Clicking on Add New request button driver.findElement(By.cssSelector(".admin-actions a")).click(); // #action 2: Giving input for title input WebElement requestTitleElement = driver.findElement(By.name("requestTitle")); requestTitleElement.sendKeys(titleText); // #action 3: Giving input for description field JavascriptExecutor jse = (JavascriptExecutor) driver; WebElement descriptionField = driver.findElement(By.name("request-description")); descriptionField.sendKeys("Test Description"); // #action 4: Checking multiple inputBoxes driver.findElement(By.cssSelector(".add-platform-input")).click(); List<WebElement> platformInputs = driver.findElements(By.name("sdk-tools")); List<WebElement> platformVersions = driver.findElements(By.name("sdk-tools-versions")); // #expects: Checking multiple checkboxes should have greater then one input for version nnd sdk-tool inputBox assertThat(platformInputs.size()).isGreaterThan(1); assertThat(platformVersions.size()).isGreaterThan(1); // #action 5: Add Value to Hidden Field required to Create Request jse.executeScript("$('[name=request-languages]').val('" + language + "').trigger('change');"); jse.executeScript("$('[name=sdk-tools]').val('" + sdkTool + "').trigger('change');"); // #action 6: Adding version and tags WebElement version = driver.findElement(By.name("sdk-tools-versions")); version.sendKeys("1.0"); jse.executeScript("$('input[name=tags]').val('testing').trigger('change');"); // #action 7: Clicking on the submit button driver.findElement(By.cssSelector("input[type='submit']")).click(); } /** * S87854: [Sample Exchange] Add New Sample , Check Edit Link and Delete * Link using Page Sample option */ @Test(groups = { "test", "stage" }) @NavigateTo(SAMPLE_CODE_URL) public void testAddNewSample_PasteSample() { // #action 1: Adding Sample for the paste radio option addSample("vmwareSampleTest"); // #action 2:Verifying and deleting the created sample verify_delete_AfterCreateSample(); } /** * S87854: [Sample Exchange] Add New Sample , Check Edit Link and Delete * Link Using GITHUB-GIST Option */ @Test(groups = { "test", "stage" }) @NavigateTo(SAMPLE_CODE_URL) public void testAddNewSample_gitHub_gist() { // #action 1: Clicking on Add New sample button driver.findElement(By.cssSelector(".admin-actions a")).click(); // #action 2: Selecting "Gist" option radio button WebElement radioButtonList = driver.findElement(By.cssSelector("input[value='GIST']")); radioButtonList.click(); WaitUtils.waitForAjaxComplete(driver); // #action 3: Giving input for input fields new Select(driver.findElement(By.name("sample-gist"))).selectByIndex(1); WebElement title = driver.findElement(By.name("sampleTitle")); title.sendKeys("Test GITHUB GIST"); // Waiting for 10 second to load ckeditor WebElement ckeditor = new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".cke_wysiwyg_div"))); ckeditor.click(); ckeditor.sendKeys("Test Description for GITHUB GIST"); new Select(driver.findElement(By.name("sample-language"))).selectByIndex(1); // Selecting License Option new Select(driver.findElement(By.name("sample-license"))).selectByIndex(1); // Add Value to Hidden Field required to Create Sample JavascriptExecutor jse = (JavascriptExecutor) driver; // Adding version WebElement version = driver.findElement(By.name("sdk-tools-versions")); version.sendKeys("1.0"); // Add Value to Hidden Field required to Create Sample jse.executeScript("$('[name=sdk-tools]').val('Airwatch').trigger('change');"); // Give input to the tags jse.executeScript("$('input[name=tags]').val('testing').trigger('change');"); // Agreement check driver.findElement(By.cssSelector("input[type='checkbox']")).click(); // #action 4: Clicking on submit button WebElement uploadButtonElement = driver.findElement(By.cssSelector("input[type=submit]")); uploadButtonElement.click(); String errorMessage = StringUtils.EMPTY; try { errorMessage = driver.findElement(By.cssSelector(".alert-error")).getText(); } catch (NoSuchElementException e) { // #action 5: Check for errorMessage "The submission failed because the sample already exists" if (StringUtils.isBlank(errorMessage)) { verify_delete_AfterCreateSample(); } } } /** * S87854: [Sample Exchange] Add New Sample , Check Edit Link and Delete * Link using GITHUB Repository Option */ @Test(groups = { "test", "stage" }) @NavigateTo(SAMPLE_CODE_URL) public void testAddNewSample_gitHub_repo() { // #action 1: Clicking on Add New sample button driver.findElement(By.cssSelector(".admin-actions a")).click(); // #action 2: Selecting "Repo" option radio button WebElement radioButtonList = driver.findElement(By.cssSelector("input[value='REPOSITORY']")); radioButtonList.click(); WaitUtils.waitForAjaxComplete(driver); // #action 3: Giving input for input fields // Waiting for 10 second to load ckeditors WebElement ckeditor = new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".cke_wysiwyg_div"))); ckeditor.click(); ckeditor.sendKeys("Test Description for GITHUB Repository"); List<WebElement> options = driver .findElements(By.cssSelector("select[name='sample-repository-url'] option")); assertThat(options.size()).isGreaterThan(0); new Select(new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.name("sample-repository-url")))) .selectByIndex(1); new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated((By.cssSelector(".file-browser-container")))); WebElement title = driver.findElement(By.name("sampleTitle")); title.sendKeys("Test GITHUB Repository"); new Select(driver.findElement(By.name("sample-language"))).selectByIndex(1); // Selecting License Option new Select(driver.findElement(By.name("sample-license"))).selectByIndex(1); // Add Value to Hidden Field required to Create Sample JavascriptExecutor jse = (JavascriptExecutor) driver; // Adding version WebElement version = driver.findElement(By.name("sdk-tools-versions")); version.sendKeys("1.0"); // Add Value to Hidden Field required to Create Sample jse.executeScript("$('[name=sdk-tools]').val('Airwatch').trigger('change');"); // Give input to the tags jse.executeScript("$('input[name=tags]').val('testing').trigger('change');"); // Agreement check driver.findElement(By.cssSelector("input[type='checkbox']")).click(); // #action 4: Clicking on submit button WebElement uploadButtonElement = driver.findElement(By.cssSelector("input[type=submit]")); uploadButtonElement.click(); String errorMessage = StringUtils.EMPTY; try { errorMessage = driver.findElement(By.cssSelector(".alert-error")).getText(); } catch (NoSuchElementException e) { // #action 5: Check for errorMessage "The submission failed because the sample already exists" if (StringUtils.isBlank(errorMessage)) { verify_delete_AfterCreateSample(); } } } private void verify_delete_AfterCreateSample() { WebElement contentDetailsTabHeader = driver.findElement(By.cssSelector(".span9 h1")); WebElement contentDetailsTabReadme = driver.findElement(By.cssSelector(".readme")); // #expects: Validating Content of the page assertThat(contentDetailsTabReadme).isNotNull(); assertThat(contentDetailsTabReadme.getText()).isNotEmpty(); assertThat(contentDetailsTabHeader).isNotNull(); assertThat(contentDetailsTabHeader.getText()).isNotEmpty(); WaitUtils.waitForPageLoad(driver); // #action : Deleting the sample Entry JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("$('.sample-delete-dialog').submit();"); new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#search-results-list"))); } /** * S87863: [Sample Exchange] Request New Samples , Check Edit Link and * Delete Link S87869: [Sample Exchange] Vote for Sample Requests * */ @Test(groups = { "test", "stage" }) @NavigateTo(SAMPLE_CODE_URL) public void testAddNewRequest() { // #action 1: Selecting request tab selectTab("Requests"); // Input parameters String requestTitle = "Test Request"; String language = "java"; String sdkTool = "Airwatch"; // #action 2: Adding request with Input parameters addRequest(requestTitle, language, sdkTool); // #expects: Verifying the fields with Input parameters assertThat(driver.findElement(By.cssSelector(".metadata")).getText()).contains(language); assertThat(driver.findElement(By.cssSelector(".metadata")).getText()).contains(sdkTool); assertThat(driver.findElement(By.cssSelector(".readme")).getText()).contains("Test Description"); assertThat(driver.findElement(By.cssSelector(".header-title h1")).getText()).isEqualTo(requestTitle); assertThat(driver.findElement(By.cssSelector(".tag")).getText()).isEqualTo("testing"); // #action 3: Check Post New Comment Functionality WebElement commentSection = driver.findElement(By.cssSelector(".comments")); String currentCommentValue = commentSection.findElement(By.cssSelector(".badge")).getText(); WebElement commentsTextArea = driver.findElement(By.name("request-comment")); commentsTextArea.sendKeys("Test Comment"); driver.findElement(By.cssSelector(".comments input[type='submit']")).click(); WebElement newCommentSection = driver.findElement(By.cssSelector(".comments #comments .badge")); // #expects: Verifying the comment section with commentvalue (should not be equal ) assertThat(newCommentSection.getText()).isNotEqualTo(currentCommentValue); List<WebElement> tabsAvailable = driver.findElements(By.cssSelector("#tabs .nav-tabs li a")); assertThat(tabsAvailable.size()).isGreaterThan(0); // #action 4: Select Tab : Solution JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("$('a[href=#tab-solutions]').parent().click();"); jse.executeScript("$('#tab-solutions').css('display','block');"); new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".solution-radio-form"))); // #action 5: Edit functionality test driver.findElement(By.cssSelector(".author-actions .edit a")).click(); WebElement anchor = driver.findElement(By.cssSelector(".form-horizontal input[type='submit']")); anchor.submit(); // #action 6: Deleting the request sample Entry jse.executeScript("$('.request-delete-dialog').submit();"); new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#search-results-list"))); } /** * Returns a list of URLs of the Sample Exchange links. */ @DataProvider public Object[][] getSampleExchangeUrls() { List<Object[]> data = new ArrayList<Object[]>(); // #action : Navigates to main Sample Exchange pages. driver.get(baseUrl + SAMPLE_CODE_URL); // Gets all the links under Sample page span. List<WebElement> elements = driver .findElements(By.cssSelector("#search-results-list .title-list li .title")); // We have to put this in a temporary list, since any further action on the Web driver // will make the references to the WebElement unusable. for (WebElement element : elements) { // Avoiding other URL if (!StringUtils.contains(element.getAttribute("href"), "/user?id")) { data.add(new Object[] { element.getAttribute("href").trim() }); } } return data.toArray(new Object[][] {}); } /** * S87859: [Sample Exchange] View Code sample details * */ @Test(dataProvider = "getSampleExchangeUrls") public void testSampleExchangeDetailPage(String url) { // #action 1: Navigates to the page. driver.get(url); WebElement contentDetailsTabHeader = new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".span9 h1"))); WebElement contentDetailsTabReadme = driver.findElement(By.cssSelector(".readme")); // #expects: Validating Content of the page assertThat(contentDetailsTabReadme).isNotNull(); assertThat(contentDetailsTabReadme.getText()).isNotEmpty(); assertThat(contentDetailsTabHeader).isNotNull(); assertThat(contentDetailsTabHeader.getText()).isNotEmpty(); WebElement contentDownloadButton = driver.findElement(By.cssSelector(".download a")); // #expects: Validating download button link should have the URLs assertThat(contentDownloadButton.getAttribute("href")).isNotEmpty(); List<WebElement> tabsAvailable = driver.findElements(By.cssSelector(".menu-tab")); // #expects: checking for the avilable tabs size should be greater than 0 assertThat(tabsAvailable.size()).isGreaterThan(0); // #action 2: Select Tab : Browse Code JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript(" $('#tabs ul li:nth-child(2)').click();"); new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".file-content-container"))); } /** * Returns a list of URLs of the Sample Request links. */ @DataProvider public Object[][] getSampleRequestUrls() { List<Object[]> data = new ArrayList<Object[]>(); // #action : Navigates to main Sample Exchange pages. driver.get(baseUrl + SAMPLE_CODE_URL); selectTab("Requests"); // Gets all the links under Sample page span. List<WebElement> elements = driver .findElements(By.cssSelector("#search-results-list .title-list li .title")); // We have to put this in a temporaty list, since any further action on the Webdriver // will make the references to the WebElement unusable. for (WebElement element : elements) { if (StringUtils.contains(element.getAttribute("href"), "&id=")) { data.add(new Object[] { element.getAttribute("href").trim() }); } } return data.toArray(new Object[][] {}); } /** * S87859: [Sample Exchange] View Code sample * */ @Test(dataProvider = "getSampleRequestUrls") public void testSampleRequestDetailPages(String url) { // #action 1: Navigates to the page. driver.get(url); WebElement contentDetailsTabHeader = new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".header-title h1"))); WebElement contentDetailsTabReadme = driver.findElement(By.cssSelector(".readme")); // #expects: Validating Content of the page assertThat(contentDetailsTabReadme).isNotNull(); assertThat(contentDetailsTabHeader).isNotNull(); assertThat(contentDetailsTabReadme.getText()).isNotEmpty(); assertThat(contentDetailsTabHeader.getText()).isNotEmpty(); List<WebElement> tabsAvailable = driver.findElements(By.cssSelector("#tabs .nav-tabs li a")); // #expects: checking for the avilable tabs size should be greater than 0 assertThat(tabsAvailable.size()).isGreaterThan(0); // #action 2: Select Tab : Solution JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("$('a[href=#tab-solutions]').parent().click();"); js.executeScript("$('#tab-solutions').css('display','block');"); new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".solution-radio-form"))); } /** * Returns a list of URLs of the My Sample links. */ @DataProvider public Object[][] getMySampleExchangeUrls() { List<Object[]> data = new ArrayList<Object[]>(); // #action : Navigates to main Sample Exchange pages. driver.get(baseUrl + SAMPLE_CODE_URL); selectTab("My samples"); // Gets all the links under Sample page span. List<WebElement> elements = driver .findElements(By.cssSelector("#search-results-list .title-list li .title")); // We have to put this in a temporary list, since any further action on the Web driver // will make the references to the WebElement unusable. for (WebElement element : elements) { if (!StringUtils.contains(element.getAttribute("href"), "/user?id")) { data.add(new Object[] { element.getAttribute("href").trim() }); } } return data.toArray(new Object[][] {}); } /** * S87859: [Sample Exchange] View Code sample details * */ @Test(dataProvider = "getMySampleExchangeUrls") public void testMySampleExchangeDetailPage(String url) { // #action 1: Navigates to the page. driver.get(url); WebElement contentDetailsHeader = new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".span9 h1"))); WebElement contentDetailsTabReadme = driver.findElement(By.cssSelector(".readme")); // #expects: Validating Content of the page assertThat(contentDetailsTabReadme).isNotNull(); assertThat(contentDetailsTabReadme.getText()).isNotEmpty(); assertThat(contentDetailsHeader).isNotNull(); assertThat(contentDetailsHeader.getText()).isNotEmpty(); List<WebElement> tabsAvailable = driver.findElements(By.cssSelector("#tabs .nav-tabs li a")); // #expects: checking for the avilable tabs size should be greater than 0 assertThat(tabsAvailable.size()).isGreaterThan(0); // #action 2: Select Tab : Browse Code JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript(" $('#tabs ul li:nth-child(2)').click();"); // #action 3: Checking the existence code input area driver.findElement(By.cssSelector(".divSplitter")); new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".file-content-container"))); List<WebElement> linkLists = driver.findElements(By.cssSelector(".download a")); for (WebElement link : linkLists) { // #expects: Validating download button link should have the URLs assertThat(link.getAttribute("href")).isNotEmpty(); } } /** * Returns a list of URLs of the Sample Request links. */ @DataProvider public Object[][] getMyRequestUrls() { List<Object[]> data = new ArrayList<Object[]>(); // #action : Navigates to main Sample Exchange pages. driver.get(baseUrl + SAMPLE_CODE_URL); selectTab("My Requests"); // Gets all the links under Sample page span. List<WebElement> elements = driver .findElements(By.cssSelector("#search-results-list .title-list li div a")); // We have to put this in a temporary list, since any further action on the Web driver // will make the references to the WebElement unusable. for (WebElement element : elements) { if (StringUtils.contains(element.getAttribute("href"), "request&id=")) { data.add(new Object[] { element.getAttribute("href").trim() }); } } return data.toArray(new Object[][] {}); } /** * S87859: [Sample Exchange] View Code sample * */ @Test(dataProvider = "getMyRequestUrls") public void testMyRequestDetailPages(String url) { // #action 1: Navigates to the page. driver.get(url); WebElement contentDetailsTabHeader = new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".header-title h1"))); WebElement contentDetailsTabReadme = driver.findElement(By.cssSelector(".readme")); // #expects: Validating Content of the page assertThat(contentDetailsTabHeader).isNotNull(); assertThat(contentDetailsTabHeader.getText()).isNotEmpty(); assertThat(contentDetailsTabReadme).isNotNull(); assertThat(contentDetailsTabReadme.getText()).isNotEmpty(); List<WebElement> tabsAvailable = driver.findElements(By.cssSelector("#tabs .nav-tabs li a")); assertThat(tabsAvailable.size()).isGreaterThan(0); JavascriptExecutor js = (JavascriptExecutor) driver; // #action 2: Select Tab : Solution js.executeScript("$('a[href=#tab-solutions]').parent().click();"); js.executeScript("$('#tab-solutions').css('display','block');"); new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".propose-solution-new"))); List<WebElement> solutions = driver.findElements(By.cssSelector(".tab-pane .sample-solution p a")); if (solutions.size() != 0) { for (WebElement solution : solutions) { // #expects: Validating link of the solutions assertThat(solution.getAttribute("href")).isNotEmpty(); } } } /** * S87854: [Sample Exchange] Cancel Create New sample and return */ @Test @NavigateTo(SAMPLE_CODE_URL) public void testCancelAddNewSample() { // #action 1: Select Tab : My samples selectTab("My Samples"); // #action 2: Clicking on add new sample button driver.findElement(By.cssSelector(".admin-actions a")).click(); // #action 3: Clicking on cancel button commonCancelCheck(); } /** * S87863: [Sample Exchange] Cancel Create new request and return */ @Test @NavigateTo(SAMPLE_CODE_URL) public void testCancelAddNewRequest() { // #action 1: Select Tab : My request selectTab("My Requests"); // #action 2: Clicking on add new request button driver.findElement(By.cssSelector(".admin-actions a")).click(); // #action 3: Clicking on cancel button commonCancelCheck(); } private void commonCancelCheck() { List<WebElement> listAnchor = driver.findElements(By.cssSelector(".form-horizontal a")); for (WebElement anchor : listAnchor) { if (anchor.getText().equalsIgnoreCase("cancel")) { anchor.click(); break; } } // #expects: samples list should not be null assertThat(new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".samples")))).isNotNull(); } /** * @param string * */ private void selectTab(String tabName) { List<WebElement> tabs = driver.findElements(By.cssSelector(".samples .navbar-inner li a")); // #expects: Validate that the Tab size assertThat(tabs.size()).as("Tab size should be greater than 1").isGreaterThan(1); for (WebElement tab : tabs) { if (tab.getText().trim().equalsIgnoreCase(tabName.trim())) { tab.click(); break; } } } /** * Check My Sample Sort Drop Down */ @Test @NavigateTo(SAMPLE_CODE_URL) public void testMySampleSortDropDown() { // #action : Select Tab : My sample selectTab("My Samples"); checkResultSortDropDown(); } /** * Check My Request Sort Drop Down */ @Test @NavigateTo(SAMPLE_CODE_URL) public void testMyRequestSortDropDown() { // #action : Select Tab : My request selectTab("My Requests"); checkResultSortDropDown(); } private void checkResultSortDropDown() { WebElement sortDropElement = driver.findElement(By.cssSelector("#search-results-sort-by")); WebElement sampleCount = driver.findElement(By.cssSelector("#search-results-list strong")); if (!sampleCount.getText().equals("0")) { Select dropdown = new Select(sortDropElement); int dropDownListValue = dropdown.getOptions().size(); for (int i = 1; i < dropDownListValue; i++) { dropdown = new Select(driver.findElement(By.cssSelector("#search-results-sort-by"))); dropdown.selectByIndex(i); _log.debug("Dropdown selected with index " + i); assertThat(new WebDriverWait(driver, 30).until( ExpectedConditions.presenceOfElementLocated(By.cssSelector("#search-results-list ul li")))) .isNotNull(); assertThat(driver.findElement(By.cssSelector("#search-results-sort-by"))).isNotNull(); } } } /** * Check Language Filters Available */ @Test @NavigateTo(SAMPLE_CODE_URL) public void testCheckAvailableLanguageFilters() { new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".samples"))); List<WebElement> sampleLanguageList = driver.findElements(By.name("languages-for-samples")); List<String> sampleLanguages = new ArrayList<String>(); for (WebElement element : sampleLanguageList) { String currentElementValue = element.getAttribute("value"); if (currentElementValue.equals("")) { currentElementValue = "All"; } sampleLanguages.add(currentElementValue); } driver.findElement(By.cssSelector(".admin-actions a")).click(); Select sampleDropdown = new Select(driver.findElement(By.cssSelector("select[name='sample-language']"))); for (WebElement dropDownOption : sampleDropdown.getOptions()) { if (!dropDownOption.getAttribute("value").contains("Select a Language")) { assertThat(sampleLanguages.contains(dropDownOption.getAttribute("value"))).isTrue(); } } } /** * S87859: [Sample Exchange] Add Request Solutions - Snippet * */ @Test(groups = { "test", "stage" }) @NavigateTo(SAMPLE_CODE_URL) public void testAddSolutionAsSnippet() { // #action 1: Navigates to the page. selectTab("Requests"); addRequest("Rtest", "java", "Airwatch"); new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#tabs .nav-tabs li a"))); // Show all Tabs JavascriptExecutor js = (JavascriptExecutor) driver; List<WebElement> tabsAvailable = driver.findElements(By.cssSelector("#tabs .nav-tabs li")); assertThat(tabsAvailable.size()).isGreaterThan(0); // #action 2: Select Tab : Solution js.executeScript("$('a[href=#tab-solutions]').parent().click();"); js.executeScript("$('#tab-solutions').css('display','block');"); new WebDriverWait(driver, 30).until(ExpectedConditions .presenceOfElementLocated(By.cssSelector(".solution-radio-form input[type='radio'][value='new']"))) .click(); // #action 3 : Click on New Sample button to open submit solutions page js.executeScript("$('.propose-solution-new').submit();"); // #action 4: Adding sample solution new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".samples h1"))); driver.findElement(By.cssSelector("input[value='SNIPPET']")).click(); js.executeScript("$('textarea[name=sample-snippet-content]').val('This is a Sample Snippet');"); WebElement ckeditor = new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".cke_wysiwyg_div"))); ckeditor.click(); ckeditor.sendKeys("Test Description"); new Select(driver.findElement(By.name("sample-language"))).selectByIndex(1); WebElement sampleSnippetTitle = driver .findElement(By.cssSelector("input[type='text'][name='sampleTitle']")); sampleSnippetTitle.sendKeys("Solution-Test"); new Select(driver.findElement(By.name("sample-license"))).selectByIndex(1); new WebDriverWait(driver, 30) .until(ExpectedConditions .presenceOfElementLocated(By.cssSelector("input[type='checkbox'][name='agreement']"))) .click(); WebElement uploadButtonElement = driver.findElement(By.cssSelector("input[type=submit]")); WebElement sampleTitle = driver.findElement(By.cssSelector("input[name=sampleTitle]")); uploadButtonElement.click(); String errorMessage = StringUtils.EMPTY; // #action 5: verifying the tabs try { errorMessage = driver.findElement(By.cssSelector(".alert-error")).getText(); // #action 6: Nevigate to My Request Page driver.get(baseUrl + SAMPLE_CODE_URL); selectTab("My Requests"); // #action 7 : Selecting latest first Select dropdown = new Select(driver.findElement(By.cssSelector("#search-results-sort-by"))); dropdown.selectByIndex(2); List<WebElement> requestLinkList = driver .findElements(By.cssSelector("#search-results-list .title-list li")); for (WebElement requestlink : requestLinkList) { String linkHeadText = requestlink.findElement(By.cssSelector("div a")).getText(); if (linkHeadText.equals("Rtest")) { requestlink.click(); requestlink.findElement(By.cssSelector("div a")).click(); // #action 8: Deleting created JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("$('.request-delete-dialog').submit();"); new WebDriverWait(driver, 10).until( ExpectedConditions.presenceOfElementLocated(By.cssSelector("#search-results-list"))); break; } } } catch (Exception e) { if (StringUtils.isBlank(errorMessage)) { deleteSolution(sampleTitle.getText()); WaitUtils.waitForPageLoad(driver); js.executeScript("$('.request-delete-dialog').submit();"); } } } /** * [Sample Exchange] View Request Solutions - GIST */ @Test(dataProvider = "getRequestUrl") public void testAddSolutionAsSnippetAsGitHubGist(String url) { // #action 1: Navigates to the page. driver.get(url); new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".header-title h1"))); new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#tabs .nav-tabs li a"))); // Show all Tabs JavascriptExecutor js = (JavascriptExecutor) driver; List<WebElement> tabsAvailable = driver.findElements(By.cssSelector("#tabs .nav-tabs li")); assertThat(tabsAvailable.size()).isGreaterThan(0); // #action 2: Select Tab : Solution js.executeScript("$('a[href=#tab-solutions]').parent().click();"); js.executeScript("$('#tab-solutions').css('display','block');"); // #action 3 : Select Radio Button for New Sample new WebDriverWait(driver, 30).until(ExpectedConditions .presenceOfElementLocated(By.cssSelector(".solution-radio-form input[type='radio'][value='new']"))) .click(); // #action 4 : Click on New Sample button to open submit solutions page js.executeScript("$('.propose-solution-new').submit();"); // #action 5: Adding sample solution new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".samples h1"))); // #action 6: Selecting "Gist" option radio button WebElement radioButton = driver.findElement(By.cssSelector("input[value='GIST']")); radioButton.click(); try { new WebDriverWait(driver, 10).until(ExpectedConditions .presenceOfElementLocated((By.cssSelector("select[name='sample-gist'] option")))); // #action 7: Giving input for input fields Select gistDropDown = new Select(driver.findElement(By.name("sample-gist"))); gistDropDown.selectByIndex(1); } catch (Exception e) { } WebElement title = driver.findElement(By.name("sampleTitle")); title.sendKeys("Test GITHUB GIST"); // Waiting for 10 second to load ckeditor WebElement ckeditor = new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".cke_wysiwyg_div"))); ckeditor.click(); ckeditor.sendKeys("Test Description for GITHUB GIST"); new Select(driver.findElement(By.name("sample-language"))).selectByIndex(1); // Selecting License Option new Select(driver.findElement(By.name("sample-license"))).selectByIndex(1); // Add Value to Hidden Field required to Create Sample JavascriptExecutor jse = (JavascriptExecutor) driver; // Adding version WebElement version = driver.findElement(By.name("sdk-tools-versions")); version.sendKeys("1.0"); // Add Value to Hidden Field required to Create Sample jse.executeScript("$('[name=sdk-tools]').val('Airwatch').trigger('change');"); // Give input to the tags jse.executeScript("$('input[name=tags]').val('testing').trigger('change');"); // Agreement check driver.findElement(By.cssSelector("input[type='checkbox']")).click(); // #action 8: Clicking on submit button WebElement uploadButtonElement = driver.findElement(By.cssSelector("input[type=submit]")); WebElement sampleTitle = driver.findElement(By.cssSelector("input[name=sampleTitle]")); uploadButtonElement.click(); String errorMessage = StringUtils.EMPTY; try { errorMessage = driver.findElement(By.cssSelector(".alert-error")).getText(); } catch (NoSuchElementException e) { // #action 9: Check for errorMessage // "The submission failed because the sample already exists" if (StringUtils.isBlank(errorMessage)) { deleteSolution(sampleTitle.getText()); } } } /** * S87854: [Sample Exchange] Add New Sample , Check Edit Link and Delete * Link using GITHUB Repository Option */ @Test(dataProvider = "getRequestUrl") public void testAddSolutionAsSnippetAsGitHubRepo(String url) { // #action 1: Navigates to the page. driver.get(url); new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".header-title h1"))); new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#tabs .nav-tabs li a"))); // Show all Tabs JavascriptExecutor js = (JavascriptExecutor) driver; List<WebElement> tabsAvailable = driver.findElements(By.cssSelector("#tabs .nav-tabs li")); assertThat(tabsAvailable.size()).isGreaterThan(0); // #action 2: Select Tab : Solution js.executeScript("$('a[href=#tab-solutions]').parent().click();"); js.executeScript("$('#tab-solutions').css('display','block');"); // #action 3 : Select Radio Button for New Sample WebElement newSampleRadioBtn = new WebDriverWait(driver, 30).until(ExpectedConditions .presenceOfElementLocated(By.cssSelector(".solution-radio-form input[type='radio'][value='new']"))); newSampleRadioBtn.click(); // #action 4 : Click on New Sample button to open submit solutions page js.executeScript("$('.propose-solution-new').submit();"); // #action 5: Selecting "Repo" option radio button WebElement radioButtonList = driver.findElement(By.cssSelector("input[value='REPOSITORY']")); radioButtonList.click(); try { new WebDriverWait(driver, 10).until(ExpectedConditions .presenceOfElementLocated((By.cssSelector("select[name='sample-repository-url'] option")))); // #action 6: Giving input for input fields Select gistDropDown = new Select(driver.findElement(By.name("sample-repository-url"))); gistDropDown.selectByIndex(1); } catch (Exception e) { } WebElement title = driver.findElement(By.name("sampleTitle")); title.sendKeys("Test GITHUB Repository"); // Waiting for 10 second to load ckeditor WebElement ckeditor = new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".cke_wysiwyg_div"))); ckeditor.click(); ckeditor.sendKeys("Test Description for GITHUB Repository"); new Select(driver.findElement(By.name("sample-language"))).selectByIndex(1); // Selecting License Option new Select(driver.findElement(By.name("sample-license"))).selectByIndex(1); // Add Value to Hidden Field required to Create Sample JavascriptExecutor jse = (JavascriptExecutor) driver; // Adding version WebElement version = driver.findElement(By.name("sdk-tools-versions")); version.sendKeys("1.0"); // Add Value to Hidden Field required to Create Sample jse.executeScript("$('[name=sdk-tools]').val('Airwatch').trigger('change');"); // Give input to the tags jse.executeScript("$('input[name=tags]').val('testing').trigger('change');"); // Agreement check driver.findElement(By.cssSelector("input[type='checkbox']")).click(); // #action 7: Clicking on submit button WebElement uploadButtonElement = driver.findElement(By.cssSelector("input[type=submit]")); WebElement sampleTitle = driver.findElement(By.cssSelector("input[name=sampleTitle]")); uploadButtonElement.click(); String errorMessage = StringUtils.EMPTY; try { errorMessage = driver.findElement(By.cssSelector(".alert-error")).getText(); } catch (NoSuchElementException e) { // #action 8: Check for errorMessage // "The submission failed because the sample already exists" if (StringUtils.isBlank(errorMessage)) { deleteSolution(sampleTitle.getText()); } } } private void deleteSolution(String sampleTitle) { JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("$('a[href=#tab-solutions]').parent().click();"); js.executeScript("$('#tab-solutions').css('display','block');"); List<WebElement> existingSolutions = driver.findElements(By.cssSelector(".sample-solution")); for (WebElement solution : existingSolutions) { WebElement existingSolutionsAnchors = solution.findElement(By.cssSelector(".name")); if (existingSolutionsAnchors.getText().contains(sampleTitle)) { // #action : Test Delete Solution js.executeScript("$('.solution-delete-dialog').submit();"); new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".header-title"))); break; } } } /** * Returns a list of URLs of the Sample Request links. */ @DataProvider public Object[][] getRequestUrl() { List<Object[]> data = new ArrayList<Object[]>(); // #action : Navigates to main Sample Exchange pages. driver.get(baseUrl + SAMPLE_CODE_URL); selectTab("Requests"); // Gets all the links under Sample page span. List<WebElement> elements = driver .findElements(By.cssSelector("#search-results-list .title-list li .title")); // We have to put this in a temporary list, since any further action on // the Web driver // will make the references to the WebElement unusable. for (WebElement element : elements) { data.add(new Object[] { element.getAttribute("href").trim() }); break; } return data.toArray(new Object[][] {}); } }