Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am using getfirstselectedoption.gettext() method to get the default selected value in drop down which helps to reduce execution time as i need to select the value in that drop down every time. But it's taking approx 15 to 20 secs to get that default selected value. Drop down contains nearly 180 string values. I don't understand why it's taking that much time. Any help would be appreciated.
Looking through the Selenium API, and associated source tells you: .getFirstSelectedOption() is:
public WebElement getFirstSelectedOption() {
for (WebElement option : getOptions()) {
if (option.isSelected()) {
return option;
}
}
throw new NoSuchElementException("No options are selected");
}
and getOptions() is:
/**
* #return All options belonging to this select tag
*/
public List<WebElement> getOptions() {
return element.findElements(By.tagName("option"));
}
So your expectation that at the first hit the loop will stop is not correct; it has to fetch all your options first.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
Background: I have a very large dataset which logs what settings Users have saved.
I wrote some code which logs if a user has changed any settings from day one to day 2. The following are two functions I wrote which are part to make this happen:
packages used are 'duckdb' and 'dplyr'
p_loop <- function(p){
progress <- paste(p,"out of",nrow(alluser_list), sep =" ")
write.table(progress, "progress_estimation.csv")
user1_day1 <- dbGetQuery(con, paste("SELECT * FROM demo_day1 WHERE userId = '",alluser_list[p,],"'",sep = ""))
user1_day2 <- dbGetQuery(con, paste("SELECT * FROM demo_day2 WHERE userId = '",alluser_list[p,],"'",sep = ""))
#from first day, make list with all locations
user1_locations <- user1_day1 %>% distinct(name)
o_loop <- function(o){
user1_day1_location_o <- user1_day1[user1_day1[,6]==user1_locations[o,],c(7,8,9,10)]
user1_day2_location_o <- user1_day2[user1_day2[,6]==user1_locations[o,],c(7,8,9,10)]
arrange(user1_day1_location_o, warnType)
arrange(user1_day2_location_o, warnType)
user1_day2_location_o
if(isTRUE(all_equal(user1_day1_location_o,user1_day2_location_o))){
numticker <- 0
} else{
numticker <- 1
}
###
###
#check if numticker is greater than 0. If yes at least one change in settings occured for this user and this location.
#record result in table Results. Records UserId, location and if setting change occured(0/1)
if(numticker>0){
results <- data.frame(alluser_list[p,],user1_locations[o,],1)
colnames(results) <- c("UserId","Location","Result")
dbAppendTable(con,"Results",results)
} else {
results <- data.frame(alluser_list[p,],user1_locations[o,],0)
colnames(results) <- c("UserId","Location","Result")
dbAppendTable(con,"Results",results)
}
}
lapply(1:nrow(user1_locations),o_loop)
}
lapply(1:nrow(alluser_list),p_loop)
My code works but it is way too slow. On the cluster, that I work with, one iteration of p_loop takes about 14 seconds. There are a total of 3.5 million iterations. So I need a way to speed this up by magnitudes. I already reworked the code to use functions and lapply instead of for loops (hence the _loop names, because I learned that for loops are very inefficient. This saved about 4 seconds per iteration so not nearly enough to make this usable.
I'm asking this question because I'm unsure if this is even possible.
My knowledge of R is super basic.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a variable with a list on it and I need to use its value for my find option. I get an error when I set my id_user to id_u.
Here is the list
id_u = user_key[0]
This is my SELECT and WHERE
find = ("SELECT * FROM hashtags WHERE id_user=id_u")
You have to concatenate SELECT string with variable value.
Try like this:
id_u = user_key[0]
find = ("SELECT * FROM hashtags WHERE id_user=" + id_u)
We have a component which takes more time to build and we want to test it with Karate:
retry(120, 1000).waitForText('h1', 'Successful')
There can be more states in the h1 element (Waiting, Building), but error is usually shown almost immediately. How can we put waitForText to the condition and diferenciace between these 2 final states? Something like this
retry(120, 1000).if (waitForText('h1', 'Successful')) { ...continue with tests } elseif(waitForText('h1', 'Error')) { karate.fail('Error occured') }
Please read the docs for optional() and exists(): https://github.com/intuit/karate/tree/master/karate-core#optional
Also it is quite likely waitForAny() will solve this: https://github.com/intuit/karate/tree/master/karate-core#waitforany
* retry(120, 1000).waitForAny('{h1}Successful', '{h1}Error')
* if exists('{h1}Error') karate.fail('error occurred')
The first line above will actually return the Element so you can be smarter on the second line. There are many options, find the way that suits you best.
I have two tables :
Question table where contains :
And then I have another table that is called
UserAnswerQuestion that contains :
That has reference to the question via question_id
The thing is that every time user answers a question I create a row in db saying :
question_id user_id has answered it and then I check if the answer is correct or not, if it's correct I put passed as a True, otherwise I put passed as a false.
Ok, from now is all ok, the thing that I can not get is I'm creating a method that returns to me the nextQuestion not answered, but I'm not able to do this method correctly.
What I'd like is to have a query that first return all questions that user have not answered yet, and then another query that returns just a question that user has not answered yet.
Note: This shown attribute was added because I want first to iterate over other questions instead of showing the ones that user has failed recently.
Is the question clear?
What I've tried?
I've tried to get the questions List<UserAnswerQuestion> findAllByUserEmailAndPassedFalseAndShownFalse(String email); but it doesn't work because with this table I can know every answer in every try that user does, so for example if I have a question that have 4 answers and 1 correct, this table can contain 4 row with the same question_question_id because the user perhaps has failed 3 times and then at the fourth it answers correctly.
Edit
I have already the questions that user can answer with this method :
public List<Question> getAllQuestionsThatUserCanAnswer(String email, Long topic){
List<Question> mList = this.questionRepository.findAllByTopicParentId(topic);
if(mList==null) return null;
List<UserAnswerQuestion> userAnswerQuestionList = this.userAnswerQuestionRepository.findAllByUserEmail(email);
for (UserAnswerQuestion userAnswerQuestion : userAnswerQuestionList) {
if(mList.contains(userAnswerQuestion.getQuestion())){
if(userAnswerQuestion.getPassed()){
mList.remove(userAnswerQuestion.getQuestion());
}
}
}
return mList;
}
Now I'm trying to do the getNext of this method but I do not know how to iterate over it.
Focus on SQL query that solve your problem
select * from Question q
left join Answer a on q.id = a.question_id
where a.id is null OR (a.passed != 'true' AND a.user_id = 1)
order by a.passed
Check scratch here
I have a question and answer model, answers belong to questions and a question has many answers. Answers have a boolean :correct column so answers can be marked as correct.
The code below checks whether a question has any correct answers and then displays a different div accordingly. The code works however it performs a laborious count query on the database which I would like to avoid.
Is there a way to rewrite this query or is it best to have a column in the questions table which is toggled to true when an answer is marked as correct?
<div class="<%= question.answers.count(conditions: ['correct = ?', true]) == 1 ? 'correct-answer' : 'no-correct-answer' %>">
<%= question.answers_count %>
</div>
EDIT
Thanks to the guys below who posted answers, however even with an questions_id index on the answers table, both queries were using a count query which i want to avoid, as there is likely to be quite a few answers to have to loop through.
My solution was to create a :correct boolean column on the question table, and when an answer is marked/toggled as correct it toggled this column as well, so when rendering a view i didnt have to perform any dynamic SQL queries.
answer.rb
def toggle_correct(attribute)
toggle(attribute).update_attributes({attribute => self[attribute]})
end
def toggle_question_correct
self.question.toggle_correct(:correct)
end
You're counting how many correct answers there is for the question (i.e. check every answer), whereas you only need to check if a correct answer exists (i.e. stop as soon as the correct answer is spotted). This could be written with exists?
question.answers.exists?(:correct => true)
Unless you have a lot of answers for each question, it should not change significantly the response time. You said this part of code was 'laborious', you should check that you created an index on the column question_id of the table answers. Without it, question.answers generate a full scan of the answers table.
I would suggest that you create two scopes on Answer model
scope :correct, -> { where(correct: true) }
scope :correct, -> { where(correct: false) }
then you may select count correnct answers for question like that:
question.answers.correct.count
You might also want to create a method on Question
def has_correct_answer?
! answers.correct.count.zero?
end
If you're showing many questions and answers on single page I would suggest going for AR#includes with where clause so it will make less SQL queries (http://guides.rubyonrails.org/active_record_querying.html#eager-loading-multiple-associations)