Can you make a JOptionPane.showMessageDialog appear after a duration of time? - java

I have a question regarding how someone was to make a JOptionPane.showMesageDialog(); appear within a duration of time. For example, my program is supposed to ask about the user's favorite movie, Subsequently at least 15 seconds after the user answers, my program is supposed to ask "are you there?", giving them the option of replying with Yes, no, or maybe.
How exactly can I do that? (if that's possible)
here's my code
```
if (null==moviename|| moviename.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "You did not enter anything");
} else { JOptionPane.showMessageDialog(null, moviename + ", sounds like a good watch.");
}
String ques=JOptionPane.showInputDialog("Are you there? Yes? No? Maybe?");
if (ques=="yes") {
JOptionPane.showMessageDialog(null,"Great To hear!");
} else if (ques=="no") {
JOptionPane.showMessageDialog(null,"Thats odd..");
} else if (ques=="maybe" || ques=="Maybe" ) {
JOptionPane.showMessageDialog(null, "That was rhetorical...");
}
}
```

I belive you just need to add Thread.sleep(n); before the JOptionPane
remember to import: java.lang.Thread;
time is in ms so 15seconds is gonna be 15000.
if (null==moviename|| moviename.trim().isEmpty()) {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
System.out.print("Sleep problem of type: "+e);
}
JOptionPane.showMessageDialog(null, "You did not enter anything");
} else {
JOptionPane.showMessageDialog(null, moviename + ", sounds like a good watch.");
}

Related

How to check that frame does not contain jlabel with specific text with help of Assertj?

I want to test my simple application with AssertJ.
I can check that text is showing like that:
frame.label(JLabelMatcher.withText(text).andShowing());
But is there any way to check that specific text not showing?
As a result, I solved this issue in such a not pretty way:
public void iDontSeeText(String text) {
try {
frame.label(JLabelMatcher.withText(Pattern.compile(".*" + text + ".*", Pattern.CASE_INSENSITIVE)).andShowing());
} catch (Exception e) {
// Control isn't found. Test complete successful
return;
}
// Control is found. Execute exception
throw new RuntimeException("Text \"" + text + "\" is found in frame.");
}

I am not able to toast messages in Android Studio

I have created an add button. No data is entered in the EditText field and when the user press the button I am not able to Toast the message.
If(text1.getText( ).toString( ).matches(" ") || text2.getText( ).toString( ).matches(" "))
{
Toast.makeText(MainActivity.this,"input values",Toast.LENGTH_SHORT.show( );
}
you can try this
EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_SHORT).show();
}
You are matching wrong string. Instead of .match(" ") use .match("") or it is better to use text1.getText().toString().isEmpty(). In fact your if block never reached.
Try changing your code and IF condition like this:
If (String.valueOf(text1.getText()).equals("") || String.valueOf(text2.getText()).equals(""))
{
Toast.makeText(MainActivity.this,"input values",Toast.LENGTH_SHORT).show();
}
That's all,
Hope it helps :)
Use trim() this function will remove spaces.
If(text1.getText( ).toString( ).trim().equals("") || text2.getText( ).toString.trim().equals(""))
{
Toast.makeText(MainActivity.this,"You did not enter a username and password",Toast.LENGTH_SHORT.show( );
}
I hope this will helpful.

Selenium: how to write if condition when there is no element for some xpath

Below is the code and question:
if(result.findElement(By.xpath(".//*[#class='s-item-container']/div[4]/span/a/i")).getText().equals("Lightning Deal"))
After we search any product on Amazon, we can also see the "Lighting Deal" option on some products. Now here I am trying to check if the lighting deal option is present or not. If it is present then condition is true and everything is okay. But if there is no lighting deal, then this gives the error NoSuchElementException because then div[4]/span/a/i doesn't exist. This div[4]/span/a/i comes into life only when there is some lighting deal.
Please suggest me how to write this if condition.
Complete Code:
//All the products after searching in List
List<WebElement> resultsList = driver.findElements(By.xpath(".//*[starts-with(#id, 'result_')]"));
for(WebElement result:resultsList)
{
System.out.println("Name of the product:"+result.findElement(By.tagName("h2")).getText());
System.out.println("Brand: "+result.findElement(By.xpath(".//*[#class='s-item-container']/div[3]/div[2]/span[2]")).getText());
if(result.findElement(By.xpath(".//*[#class='s-item-container']/div[4]/span/a/i")).getText().equals("Lightning Deal"))
{
String sp =result.findElement(By.xpath(".//*[#class='s-item-container']/div[5]/div[1]/a/span[2]")).getText();
System.out.println("Selling Price: "+sp);
}
else
{
System.out.println("Selling Price:"+result.findElement(By.xpath(".//*[#class='s-item-container']/div[4]/div[1]/a/span[2]")).getText());
}
}
you can check the element is present on page before getting text
for(WebElement result:resultsList)
{
System.out.println("Name of the
product:"+result.findElement(By.tagName("h2")).getText());
System.out.println("Brand: "+result.findElement(By.xpath(".//*[#class='s-
item-container']/div[3]/div[2]/span[2]")).getText());
if(result.findElements(By.xpath(".//*[#class='s-item-
container']/div[4]/span/a/i")).size() != 0
&& result.findElement(By.xpath(".//*[#class='s-item-
container']/div[4]/span/a/i")).getText().equals("Lightning Deal"))
{
String sp =result.findElement(By.xpath(".//*[#class='s-item-
container']/div[5]/div[1]/a/span[2]")).getText();
System.out.println("Selling Price: "+sp);
}
else
{
System.out.println("Selling Price:"+result.findElement(By.xpath(".//*
[#class='s-item-container']/div[4]/div[1]/a/span[2]")).getText());
}
}
This is what you need to do
List<WebElement> deals = result.findElements(By.xpath(".//*[#class='s-item-container']/div[4]/span/a/i"));
if(deals.size() > 0)
{
if(deals.get(0).getText().equals("Lightning Deal"))
{
String sp =result.findElement(By.xpath(".//*[#class='s-item-container']/div[5]/div[1]/a/span[2]")).getText();
System.out.println("Selling Price: "+sp);
}
else
{
System.out.println("Selling Price:"+result.findElement(By.xpath(".//*[#class='s-item-container']/div[4]/div[1]/a/span[2]")).getText());
}
}
Hope this helps
You need to enclose the if Statement in Try catch block
Enclose your if condition in try block . So if your element is there then you will be able to do your action in if condition.
If there is no such xpath then it will throw exception which will be handle under catch block so just write you else part under catch block
Refer Below code:
//All the products after searching in List
List<WebElement> resultsList = driver.findElements(By.xpath(".//*[starts-with(#id, 'result_')]"));
for(WebElement result:resultsList)
{
System.out.println("Name of the product:"+result.findElement(By.tagName("h2")).getText());
System.out.println("Brand: "+result.findElement(By.xpath(".//*[#class='s-item-container']/div[3]/div[2]/span[2]")).getText());
try
{
if(result.findElement(By.xpath(".//*[#class='s-item-container']/div[4]/span/a/i")).getText().equals("Lightning Deal"))
{
String sp =result.findElement(By.xpath(".//*[#class='s-item-container']/div[5]/div[1]/a/span[2]")).getText();
System.out.println("Selling Price: "+sp);
}
}
catch(Exception e)
{
System.out.println("Selling Price:"+result.findElement(By.xpath(".//*[#class='s-item-container']/div[4]/div[1]/a/span[2]")).getText());
}
}
This is one of my favorite hack. Basically the question is regarding, to check the element availability or not in the given page. findByElements method comes to action now. Below is the code.
if(driver.findElements(ByXpath("XPATH_HERE")).size>0)
{
//DO THE STUFF YOU NEED TO DO IF THE ELEMENT EXISTS
}
The statements inside the if block will be executed only there exists the element defined otherwise not. Hope this helps you.
Thanks.

Comparing identical strings using String.equals is found getting skipped

Below is my code
for(int j=0;j<6;j++){
//checking each deal
dealname=driver.findElement(By.id("MainContent_dtlstAllDeals_lblDealTitle_"+j)).getText();
// System.out.println(dealname);
String result[]=dealname.split("\\.");
String resultTitle=result[0];
//System.out.println(resultTitle);
String splitDealname=DealTitle.substring(0,resultTitle.length());
if(splitDealname.equals(resultTitle)){
System.out.println("***whoooooo you got it********"+j+"position"+"in"+i+"page");
//click on view deal button
driver.findElement(By.id("MainContent_dtlstAllDeals_lbtnView_"+j)).click();
Thread.sleep(5000);
//System.out.println(driver.findElement(By.id("MainContent_lblDealTitle")).getText());
String name=driver.findElement(By.id("MainContent_lblDealTitle")).getText();
//verify selected deal is correct
System.out.println(name);
//Thread.sleep(5000);
try {
if (name.equals(DealTitle)) {
System.out.println("whoos...verified");
}
/*
String statuss=veifyTitle("");
if(statuss.equals("success")){
{
System.out.println("whoos...verified");
//do buying process
}
}
else{}*/
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
}
Even though variables DealTitle and name contain same long string as below , that above code is not working.I have put the code in for loop , but when the 'if' condition is run it goes to next iteration .I found it while debugging
Detailed Interior Cleaning + Exterior Car Wash (External Foam Wash, Shampooing, Conditioning, Engine Room Wash, Tyre Polishing) Using AUTOGLYM Brand Products for Just Rs. 399 from Hasten Auto, Vennala (76% OFF)-pramod
Pls help.
I got issue solved by using replace all method
String excelTitle= DealTitle.replaceAll("[^\\w\\s\\-_]", "");
String pageTitle=name.replaceAll("[^\\w\\s\\-_]", "");
if(excelTitle.compareTo(pageTitle)==0){
System.out.println("ok strings are same");
}
Or you can use name.replaceAll("[^a-zA-Z]", "")

java_give_question_to_me

I have an aplication where is picture with SIN, COS..
TextArea1 will tell somebody: "Click in picture where SIN"
if user do this, textArea2 tell him: "It is corect"
After that, textArea1.append "Click in picture where COS"
-but program is still waiting for clicking to SIN :(
Can you help me and give a little advice to me please?
There is some code:
private class KlikaniMysi implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getX() + " " + e.getY());
//for finding coordinates (by console)
//textArea2 = "try" at start !!
//textArea1 = "Find SIN" at start !!!!!!!!
if(textArea2.equals("try")){
if( (((e.getX()) > 420)&&((e.getX()) < 580)) && ( ((e.getY()) > 164)&&((e.getY()) < 178) )){
textArea2.setText("");
textArea2.append("RIGHT1");
textArea1.append("Find COS"); //!!!! work!!
//How to stop the IF cycle here and run class KlikaniMysi (mouselistener) again ?
}
else{
textArea1.setText("");
textArea1.append("try again");
}
}else{
if( (((e.getX()) > 586)&&((e.getX()) < 594)) && ( ((e.getY()) > 174)&&((e.getY()) < 282) )){
//This isnt nice, but I dont know how to expres range better.
textArea1.setText("");
textArea2.append("Right2");
textArea1.append("Find TAN");
}
else{
vystup.setText("");
textArea1.append("Try again");
}
}
}
There are many more aspects about this code that are 'suboptimal' but I think your problem is here:
if(textArea2.equals("try"))
You want to test the content of that area, not the area itself, hence change it to
if(textArea2.getText().equals("try"))

Categories