Skip to content

Instantly share code, notes, and snippets.

@sparda2
Last active May 22, 2018 06:35
Show Gist options
  • Save sparda2/46ad54001dbaed446bd2353ef64f0706 to your computer and use it in GitHub Desktop.
Save sparda2/46ad54001dbaed446bd2353ef64f0706 to your computer and use it in GitHub Desktop.
This file has the logic to resolve the questions
exports.solveResumableQuestions = function(client) {
var incorrectQuestionIdx;
var preTestPage = client.page.mathPretest.preTestPage();
var elaStudiesPage = client.page.student.elaStudiesPage();
function answerQuestions(client, questionIdx) {
var scienceAssignmentPage = client.page.student.scienceAssignmentPage();
client.pause(1000);
scienceAssignmentPage.click(scienceAssignmentPage.getAssignmentArticleMultipleOptionAnswerFirstAnswer(questionIdx));
scienceAssignmentPage.click(scienceAssignmentPage.getAssignmentArticleMultipleOptionAnswerSubmitButton(questionIdx));
client.pause(1000);
}
function verifyAnswerIsIncorrect(client, idx) {
var scienceAssignmentPage = client.page.student.scienceAssignmentPage();
var promise = new Promise(function(resolve) {
client.pause(1000);
client.element("css selector",scienceAssignmentPage.getAssignmentArticleMultipleOptionAnswerCorrectAnswerMessage(idx),function(result) {
if (result.status != -1) {
//Question answered correctly
console.log("Question " + idx + " answered correctly");
client.pause(1000);
resolve(false);
}
else {
//Question answered incorrectly
console.log("Question " + idx + " answered incorrectly");
client.pause(1000);
resolve(true);
}
}
)})
return promise;
}
//HERE IS WHERE THE ACTUAL LOGIC TO SOLVE THE QUESTIONS STARTS
// var limit = calculateNumberQuestions(client);
var idx = 1;
var limit = 6;
var desiredAttempts = 3
var failedAttemptsCount = 0
for (idx = 1; idx <= limit; idx++) { //going through each question
// while (failedAttemptsCount<desiredAttempts){
solveQuestion(client,idx);
function solveQuestion(client, idx){
client.perform(function(){
console.log("failed attempt count outside callback: " + failedAttemptsCount);
})
answerQuestions(client, idx); //clicks answer and submits
verifyAnswerIsIncorrect(client, idx).then(function(result){
if(result == true) {
incorrectQuestionIdx = idx;
failedAttemptsCount++;
console.log("failed attempt count inside callback: " + failedAttemptsCount);
console.log("Incorrect Question id is: " + incorrectQuestionIdx);
if (failedAttemptsCount < desiredAttempts){
console.log("You still have tries - Try again!")
solveQuestion(client, idx);
}
else{
idx = 7; //trying to exit the for loop by doing this, but not working
console.log("Your desired failed number of attempts has been reached")
console.log("Your limit here is: " +limit)
console.log("your idx here is: " + idx)
}
}
})
}
// idx++;
}
var solveReadingAssessmentPartially = function(client) {
var elaAssignmentPage = client.page.student.elaAssignmentPage();
var dashboard = client.page.student.dashboard();
var data = client.globals;
client.perform(function() {
console.log("Starting to solve READING ASSIGNMENT");
});
dashboard.expect.element("@newAssignmentsNotification").to.be.visible;
dashboard.expect.element("@newAssignmentsNotification").to.be.enabled;
dashboard.click("@newAssignmentsNotification");
client.pause(500);
dashboard.expect.element("@sectionCards").to.be.visible;
dashboard.expect.element("@sectionCards").to.be.enabled;
dashboard.expect.element("@elaReadingAssignment").to.be.visible;
dashboard.expect.element("@elaReadingAssignment").to.be.enabled;
dashboard.click("@elaReadingAssignment");
client.pause(500);
client.perform(function(){ //THIS IS A NIGHTWATCH COMMAND TO ENSURE THIS CALL IS NOT MADE ASYNCHRONOUSLY BUT WHEN IT GETS TO THIS POINT. I ADDED IT BECAUSE BEFORE IT WAS TRYING TO RUN THE LOOP RIGHT AWAY.
questionUtils.solveResumableQuestions(client); //HERE IS WHERE I CALL THE FUNCTION ON THE OTHER FILE
})
elaAssignmentPage.expect.element("@rightGreenArrow").to.be.enabled;
elaAssignmentPage.expect.element("@accuracyPercentage").to.be.enabled;
elaAssignmentPage.expect.element("@pointsCoin").to.be.enabled;
elaAssignmentPage.click("@rightGreenArrow");
client.pause(500);
elaAssignmentPage.expect.element("@accomplishmentsBadge").to.be.enabled;
elaAssignmentPage.expect.element("@doneButton").to.be.enabled;
elaAssignmentPage.click("@doneButton");
client.pause(500);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment