public class QuestionTest extends EeBaseTest
{
final String COMMENT = "This is an expert answer.";
String m_questionUrl = ""; // will hold url of new question
/**
* Ask a question. Verify title text, body text, topic names
* @throws Exception
*/
@Test (groups = {"Critical"}, priority=0)
public void runAsk() throws Exception
{
String currentDateTime = TestUtilities.getTimeMmDdHhMmSsSss();
final String TITLE = "Critical WebDriver Question Title " + currentDateTime;
final String BODY = "Critical WebDriver Body Text " + currentDateTime;
final String TOPIC = "Quality Assurance";
LoginPage lp = new LoginPage(m_driver, m_eeUrl);
lp.getAndLogin(Config.asker, Config.PASSWORD);
// ask the question
AskQuestionPage aqp = new AskQuestionPage(m_driver, m_eeUrl); // Get AskQuestion page object
ViewQuestionPage vqp = aqp.submitQuestion(TITLE, BODY, TOPIC); // Submit Ask form and get back ViewQuestion object
m_questionUrl = m_driver.getCurrentUrl(); // set instance var so other tests can get to new question url
//verify the data on the view question page
assertEquals(TITLE, vqp.getTextTitle());
assertEquals(BODY, vqp.getTextBodyWhenZeroCodeSnippets());
ArrayList<String> topicNames = vqp.getTextTopics();
assertEquals(TOPIC, topicNames.get(0));
assertEquals(1, topicNames.size());
HeaderPage hp = new HeaderPage(m_driver, m_eeUrl);
hp.clickLogout();
}
/**
* Answer the question from the previous test. Verify comment text.
* @throws Exception
*/
@Test (groups = {"Critical"}, priority=1)
public void runAnswer() throws Exception
{
// log in as different user and go to question's page
LoginPage lp = new LoginPage(m_driver, m_eeUrl);
lp.getAndLogin(Config.expert, Config.PASSWORD);
m_driver.get(m_questionUrl);
// comment on question
ViewQuestionPage vqp = new ViewQuestionPage(m_driver, m_eeUrl);
vqp.sendKeysCommentTextInput(COMMENT);
vqp.clickSubmitButton();
// verify comment was added to question
ArrayList <String> answers = vqp.getTextAnswers();
assertEquals(1, answers.size());
assertEquals(COMMENT, answers.get(0));
HeaderPage hp = new HeaderPage(m_driver, m_eeUrl);
hp.logout();
}
/**
* Accept as solution the answer from the previous test.
* Verify the answer in the previous test is the accepted solution.
* @throws Exception
*/
@Test (groups = {"Critical"}, priority=2)
public void runAccept() throws Exception
{
// log in as question owner
LoginPage lp = new LoginPage(m_driver, m_eeUrl);
lp.getAndLogin(Config.asker, Config.PASSWORD);
m_driver.get(m_questionUrl);
// accept comment as answer
ViewQuestionPage vqp = new ViewQuestionPage(m_driver, m_eeUrl);
AcceptAnswerPage aap = vqp.clickAcceptAsAnswerButton();
aap.clickExcellentRadioButton();
vqp = aap.clickSubmitButton();
assertEquals(COMMENT, vqp.getTextAcceptedSolution());
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
/**
* Login page
*/
public class LoginPage extends Page
{
@FindBy(id="login-id2-loginForm-loginName")
WebElement m_UsernameTextInput;
@FindBy(id="login-id2-loginForm-loginPassword")
WebElement m_PasswordTextInput;
public LoginPage(WebDriver driver, String baseUrl)
{
super(driver, baseUrl, "/login.jsp");
get();
PageFactory.initElements(driver, this);
}
public LoginPage sendKeysUsername(String username)
{
m_UsernameTextInput.sendKeys(username);
return this;
}
public LoginPage sendKeysPassword(String password)
{
m_PasswordTextInput.sendKeys(password);
return this;
}
public ViewHomePage submit()
{
m_PasswordTextInput.submit();
return new ViewHomePage(m_driver, m_baseUrl);
}
public void login(String username, String password)
{
sendKeysUsername(username);
sendKeysPassword(password);
submit();
}
public void getAndLogin(String username)
{
getAndLogin(username, "test");
}
public void getAndLogin(String username, String password)
{
get();
login(username, password);
}
}
public class EeBaseTest
{
protected String m_eeUrl;
protected String m_driverType;
protected WebDriver m_driver;
/**
* Set parameters for the test
*
* @param eeUrl
* @param driverType use htmlunit or phantomjs; otherwise you get FirefoxDriver
*/
@BeforeClass(alwaysRun=true)
@Parameters ({"eeUrl", "driverType"}) // Can be set in TestNG XML file
public void setSite(@Optional("http://not_really_url.com") String eeUrl
, @Optional("firefox") String driverType)
{
m_eeUrl = getValueOrProperty(eeUrl, "eeUrl");
m_driverType = getValueOrProperty(driverType, "driverType");
newDriver(); // create a new driver of m_driverType, assign it to m_driver
}
/**
* If a property was passed on the command line, return it.
* Otherwise return the XML value
*/
public String getValueOrProperty(String xmlValue, String propertyName)
{
String propertyValue = System.getProperty(propertyName);
if (null == propertyValue)
{
return xmlValue;
}
return (null == propertyValue) ? xmlValue : propertyValue;
}
.
.
.
<suite name="Critical">
<test verbose="0" name="Critical" annotations="JDK">
<parameter name="eeUrl" value="http://some_bogus_url.com/"></parameter>
<parameter name="driverType" value="phantomjs"></parameter>
<groups><run><include name="Critical"></include></run></groups>
<classes>
<class name="com.ee.tests.views.QuestionTest"></class>
<class name="com.ee.tests.views.VideosTest"></class>
<class name="com.ee.tests.views.ArticlesTest"></class>
</classes>
</test>
</suite>
java -cp <selenium jar>;<our classes jar>;<more 3rd party jars> org.testng.TestNG <our TestNG xml file>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented: