Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Added Question and Answers functionality and basic tests #87

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.jtalks.tests.jcommune;

import org.jtalks.tests.jcommune.webdriver.action.QuestionAndAnswersTopic;
import org.jtalks.tests.jcommune.webdriver.action.Topics;
import org.jtalks.tests.jcommune.webdriver.action.Users;
import org.jtalks.tests.jcommune.webdriver.entity.topic.QuestionAndAnswers;
import org.jtalks.tests.jcommune.webdriver.entity.user.User;
import org.jtalks.tests.jcommune.webdriver.exceptions.ValidationException;
import org.jtalks.tests.jcommune.webdriver.page.QuestionAndAnswersPage;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.jtalks.tests.jcommune.webdriver.JCommuneSeleniumConfig.driver;
import static org.jtalks.tests.jcommune.webdriver.page.Pages.mainPage;

public class QuestionAndAnswersTest {

@BeforeClass(alwaysRun = true)
@Parameters({"appUrl"})
public void enablingQuestionAndAnswersPlugin (String appUrl) throws Exception {
driver.get(appUrl);
mainPage.logOutIfLoggedIn(driver);
Users.signIn(User.admin());
mainPage.openPluginsPage();
mainPage.ensurePlugingEnabled();
}


@BeforeMethod(alwaysRun = true)
@Parameters({"appUrl"})
public void setupCase(String appUrl) throws ValidationException {
driver.get(appUrl);
mainPage.logOutIfLoggedIn(driver);
}


@Test(groups = "ui-tests")
public void userCanCreateQuestionWithValidTitleAndBody_ShouldPass() throws Exception {
Users.signUpAndSignIn();
QuestionAndAnswers question = new QuestionAndAnswers();
QuestionAndAnswers createdTopic = QuestionAndAnswersTopic.createQuestionAndAnswers(question);
Assert.assertTrue(Topics.isCreated(createdTopic));
}

@Test(groups = "ui-tests")
public void postValidCommentToOwnQuestion_ShouldPass() throws Exception {
Users.signUpAndSignIn();
QuestionAndAnswers question = new QuestionAndAnswers();
String comment = (randomAlphanumeric(200));
QuestionAndAnswersTopic.createQuestionAndAnswers(question);
QuestionAndAnswersTopic.fillCommentToQuestion(question, comment);
}

@Test(groups = "ui-tests")
public void editCommentToOwnQuestion_ShouldPass() throws Exception {
Users.signUpAndSignIn();
QuestionAndAnswers question = new QuestionAndAnswers();
String comment = (randomAlphanumeric(200));
QuestionAndAnswersTopic.createQuestionAndAnswers(question);
QuestionAndAnswersTopic.fillCommentToQuestion(question, comment);
QuestionAndAnswersTopic.editCommentToQuestion(question, comment);
}

@Test(groups = "ui-tests")
public void deleteCommentToOwnQuestion_ShouldPass() throws Exception {
Users.signUpAndSignIn();
QuestionAndAnswers question = new QuestionAndAnswers();
String comment = (randomAlphanumeric(200));
QuestionAndAnswersTopic.createQuestionAndAnswers(question);
QuestionAndAnswersTopic.fillCommentToQuestion(question, comment);
QuestionAndAnswersTopic.deleteQAComment(question, comment);
Thread.sleep(100);
Assert.assertFalse(driver.getPageSource().contains(comment), "The comment is still present on the page");
}

@Test(groups = "ui-tests")
public void addAnswerToOwnQuestion_ShouldPass() throws Exception {
Users.signUpAndSignIn();
QuestionAndAnswers question = new QuestionAndAnswers();
QuestionAndAnswersTopic.createQuestionAndAnswers(question);
QuestionAndAnswersTopic.answerToQuestion(question);

}
}
14 changes: 8 additions & 6 deletions functional-tests-jcommune/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<suite name="JCommune">
<parameter name="appUrl" value="http://autotests.jtalks.org/jcommune/"/>
<parameter name="webDriverUrl" value="http://selenium-server.jtalks.org/wd/hub"/>

<listeners>
<!--Creates a selenium session for tests to use-->
<listener class-name="org.jtalks.tests.jcommune.webdriver.SeleniumSessionListener"/>
Expand Down Expand Up @@ -31,12 +32,13 @@
<class name="org.jtalks.tests.jcommune.AdministrationTest"/>
<class name="org.jtalks.tests.jcommune.CodeReviewTest"/>
<class name="org.jtalks.tests.jcommune.PrivateMessagesTest"/>
</classes>
</test>
<!--
This takes long time to run, usually will be run only headless (without browser) to speed up. It won't be run by
default, only Smokes are run. To run these tests pass to maven: -Dtestnames=FullRegression. See pom.xml for more
information.-->
<class name="org.jtalks.tests.jcommune.QuestionAndAnswersTest"/>
</classes>
</test>
<!--
This takes long time to run, usually will be run only headless (without browser) to speed up. It won't be run by
default, only Smokes are run. To run these tests pass to maven: -Dtestnames=FullRegression. See pom.xml for more
information.-->

<test name="FullRegression">
<groups>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.jtalks.tests.jcommune.webdriver.action;

import org.jtalks.tests.jcommune.assertion.Existence;
import org.jtalks.tests.jcommune.webdriver.JCommuneSeleniumConfig;
import org.jtalks.tests.jcommune.webdriver.entity.branch.Branch;
import org.jtalks.tests.jcommune.webdriver.entity.topic.QuestionAndAnswers;
import org.jtalks.tests.jcommune.webdriver.exceptions.CouldNotOpenPageException;
import org.jtalks.tests.jcommune.webdriver.exceptions.PermissionsDeniedException;
import org.jtalks.tests.jcommune.webdriver.exceptions.ValidationException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import ru.yandex.qatools.allure.annotations.Step;
import java.util.List;

import static org.apache.commons.collections.CollectionUtils.isEmpty;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
import static org.jtalks.tests.jcommune.utils.ReportNgLogger.info;
import static org.jtalks.tests.jcommune.webdriver.JCommuneSeleniumConfig.driver;
import static org.jtalks.tests.jcommune.webdriver.action.Topics.openRequiredTopic;
import static org.jtalks.tests.jcommune.webdriver.page.Pages.*;
import static org.jtalks.tests.jcommune.webdriver.page.Pages.questionAndAnswersPage;

public class QuestionAndAnswersTopic {

public static QuestionAndAnswers createQuestionAndAnswers(QuestionAndAnswers questionAndAnswers)
throws PermissionsDeniedException, CouldNotOpenPageException, ValidationException {
if (questionAndAnswers.getBranch() == null) {
List<WebElement> branches = sectionPage.getBranches();
if (isEmpty(branches)) {
throw new CouldNotOpenPageException("Could not open any branch, there were 0 on the page. " +
"Page URL: [" + JCommuneSeleniumConfig.driver.getCurrentUrl() + "]. " +
"Page Title: [" + JCommuneSeleniumConfig.driver.getTitle() + "]. " +
"Page source: " + JCommuneSeleniumConfig.driver.getPageSource());
}
Branch branch = new Branch(sectionPage.getBranches().get(0).getText());
questionAndAnswers.withBranch(branch);
}
Branches.openBranch(questionAndAnswers.getBranch());
branchPage.clickQuestionAndAnswers();
questionAndAnswersPage.fillQuesionAndAnswersFields(questionAndAnswers);
questionAndAnswersPage.clickAnswerToTopicButton();
assertQuestionAndAnswersFormValid();
return questionAndAnswers;
}

private static void assertQuestionAndAnswersFormValid() throws ValidationException {
String failedFields = "";
info("Check subject");
if (Existence.existsImmediately(driver, questionAndAnswersPage.getSubjectErrorMessage())) {
WebElement subjectError = questionAndAnswersPage.getSubjectErrorMessage();
failedFields += subjectError.getText() + "\n";
}
info("Check body");
if (Existence.existsImmediately(driver, questionAndAnswersPage.getBodyErrorMessage())) {
WebElement bodyError = questionAndAnswersPage.getBodyErrorMessage();
failedFields += bodyError.getText();
}
info("Check finished");
if (!failedFields.equals("")) {
info("Found validation errors: " + failedFields);
throw new ValidationException(failedFields);
}
info("Check successful. No errors.");
}

@Step
public static String fillCommentToQuestion(QuestionAndAnswers question, String comment)
throws PermissionsDeniedException, ValidationException {
openRequiredTopic(question);
info("We are in the required topic");
questionAndAnswersPage.addFirstComment(comment);
questionAndAnswersPage.clickSubmitCommentButton();
Assert.assertEquals(questionAndAnswersPage.getFirstCommentContent().getText(),comment);
return comment;
}

@Step
public static String editCommentToQuestion(QuestionAndAnswers question, String comment)
throws PermissionsDeniedException, ValidationException {
openRequiredTopic(question);
info("We are in the required topic");
questionAndAnswersPage.findComment(comment);
questionAndAnswersPage.clickEditCommentButton();
String newCommentContent = randomAlphanumeric(100);
questionAndAnswersPage.editComment(newCommentContent);
questionAndAnswersPage.clickConfirmEditCommentButton();
Assert.assertEquals(questionAndAnswersPage.getFirstCommentContent().getText(),newCommentContent);
return newCommentContent;
}

@Step
public static void deleteQAComment(QuestionAndAnswers question,String comment) throws PermissionsDeniedException {
openRequiredTopic(question);
info("We are in the required topic");
questionAndAnswersPage.findComment(comment);
info("Clicking delete button for topic's first comment");
try {
questionAndAnswersPage.clickDeleteCommentButton();;
} catch (NoSuchElementException e) {
info("Delete button was not found");
throw new PermissionsDeniedException("Delete button was not found. Lack of permissions?");
}
questionAndAnswersPage.getConfirmDeleteCommentButton().click();
}

@Step
public static void answerToQuestion(QuestionAndAnswers question) throws PermissionsDeniedException {
openRequiredTopic(question);
info("We are in the required topic");
questionAndAnswersPage.clickAnswerToQuestionButton();
questionAndAnswersPage.getConfirmAnswerButton().click();
info("Adding a new answer");
String answer = randomAlphanumeric(100);
questionAndAnswersPage.fillBody(answer);
info("Click the post answer button");
questionAndAnswersPage.getPostButton().click();
Assert.assertEquals(questionAndAnswersPage.findAnswer(answer),answer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@

package org.jtalks.tests.jcommune.webdriver.action;

import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils;
import org.jtalks.tests.jcommune.assertion.Existence;
import org.jtalks.tests.jcommune.webdriver.JCommuneSeleniumConfig;
import org.jtalks.tests.jcommune.webdriver.entity.branch.Branch;
import org.jtalks.tests.jcommune.webdriver.entity.topic.CodeReview;
import org.jtalks.tests.jcommune.webdriver.entity.topic.CodeReviewComment;
import org.jtalks.tests.jcommune.webdriver.entity.topic.Post;
import org.jtalks.tests.jcommune.webdriver.entity.topic.Topic;
import org.jtalks.tests.jcommune.webdriver.entity.topic.*;
import org.jtalks.tests.jcommune.webdriver.entity.user.User;
import org.jtalks.tests.jcommune.webdriver.exceptions.CouldNotOpenPageException;
import org.jtalks.tests.jcommune.webdriver.exceptions.PermissionsDeniedException;
import org.jtalks.tests.jcommune.webdriver.exceptions.TimeoutException;
import org.jtalks.tests.jcommune.webdriver.exceptions.ValidationException;
import static org.jtalks.tests.jcommune.webdriver.JCommuneSeleniumConfig.driver;

import org.jtalks.tests.jcommune.webdriver.page.Pages;
import org.jtalks.tests.jcommune.webdriver.page.PostPage;
import org.jtalks.tests.jcommune.webdriver.page.QuestionAndAnswersPage;
import org.jtalks.tests.jcommune.webdriver.page.TopicPage;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
Expand Down Expand Up @@ -128,7 +130,7 @@ public static void editPost(Topic topic, Post postToEdit) {
String newPostContent = randomAlphanumeric(100);
topicPage.fillBody(newPostContent);
topicPage.clickAnswerToTopicButton();
for(int i = 0; i < topic.getPosts().size(); i++) {
for (int i = 0; i < topic.getPosts().size(); i++) {
if (topic.getPosts().get(i).getPostContent().equals(postToEdit.getPostContent())) {
topic.getPosts().get(i).setPostContent(newPostContent);
break;
Expand Down Expand Up @@ -386,7 +388,7 @@ public static void editCodeReviewComment(CodeReview codeReview, CodeReviewCommen
String newCommentContent = randomAlphanumeric(100);
postPage.editCodeReviewCommentBody(newCommentContent);
postPage.clickOkButtonInEditComment();
for(int i = 0; i < codeReview.getComments().size(); i++) {
for (int i = 0; i < codeReview.getComments().size(); i++) {
if (codeReview.getComments().get(i).getPostContent().equals(codeReviewComment.getPostContent())) {
codeReview.getComments().get(i).setPostContent(newCommentContent);
break;
Expand Down Expand Up @@ -424,10 +426,11 @@ public static void editPost(CodeReview codeReview, Post postToEdit) {
throw new UnsupportedOperationException("Edit post can't be done in code review type topics");
}

public static void deleteCodeReviewComment(CodeReview codeReview, CodeReviewComment codeReviewComment){
public static void deleteCodeReviewComment(CodeReview codeReview, CodeReviewComment codeReviewComment) {
openRequiredTopic(codeReview);

postPage.clickDeleteInCodeReviewCommentContainingString(codeReviewComment.getPostContent());
postPage.closeDeleteCRCommentConfirmDialogOk();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.jtalks.tests.jcommune.webdriver.entity.topic;

public class QuestionAndAnswers extends Topic {

public QuestionAndAnswers() {
super();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class BranchPage {
@FindBy(className = "new-code-review-btn")
private WebElement newCodeReviewButton;

@FindBy(className = "new-question-btn")
private WebElement newQuestionAndAnswersButton;

@FindBy(xpath = "//table[@id='topics-table']/tbody/tr/td[@class='posts-td-small posts-td-small_2']/h2/a[contains(@href, '" + JCommuneSeleniumConfig.JCOMMUNE_CONTEXT_PATH + "/topics/')]")
private List<WebElement> topicsList;

Expand Down Expand Up @@ -80,6 +83,23 @@ public void clickCreateCodeReview() throws PermissionsDeniedException {
}
}


@Step
public void clickQuestionAndAnswers() throws PermissionsDeniedException {
info("Clicking New Question and Answers Button");
try {
if (getNewTopicToggle() != null) {
getNewTopicToggle().click();
}
getNewQuestionAndAnswersButton().click();
} catch (NoSuchElementException e) {
info("No such button found!");
throw new PermissionsDeniedException("Couldn't find New Question and Answers button. Check if Q&A plugin is enabled. Here is the page source: \n"
+ driver.getPageSource());
}
}


public boolean isUserAlreadySubscribed() {
return subscribeButton.getAttribute("href").contains("/unsubscribe");
}
Expand Down Expand Up @@ -169,6 +189,10 @@ public WebElement getNewCodeReviewButton() {
return newCodeReviewButton;
}

public WebElement getNewQuestionAndAnswersButton() {
return newQuestionAndAnswersButton;
}

public List<WebElement> getTopicsList() {
return topicsList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public class MainPage {
private WebElement administrationDropdownMenu;
@FindBy(id = "Administration")
private WebElement toggleAdmineModeLink;
@FindBy (id = "PluginPage")
WebElement togglePluginPageLink;
@FindBy (className = "plugin-checkbox")
private WebElement QuestionAndAnswersCheckbox;
@FindBy(xpath = openedDropdownMenuSel)
private WebElement openedDropdownMenu;
@FindBy(xpath = languageSwitcherSel)
Expand Down Expand Up @@ -246,6 +250,24 @@ public boolean isInSmallScreenMode() {
}
}

@Step
public void openPluginsPage() {
administrationDropdownMenu.click();
togglePluginPageLink.click();
}

@Step
public void ensurePlugingEnabled() {
info("Checking if Q&A plugin is enabled");
if (! QuestionAndAnswersCheckbox.isSelected()) {
info("Q&A plugin is disabled. Switching it ON");
QuestionAndAnswersCheckbox.click();
} else {
info("Q&A plugin is already enabled, doing nothing");
}
info("Q&A plugin is switched ON");
}

public WebElement getLastPostAuthor() {
return lastPostAuthor;
}
Expand Down
Loading