views:

73

answers:

2

I have three programs,

first does a selenium test

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import junit.framework.*;

public class MyTest extends SeleneseTestCase {

int flag_eco;

public void setUp() throws Exception {
    setUp("http://www.mysite.com/", "*iexplore");
}
public void testMyTest() throws Exception {
    selenium.open("/pages/static/homepage_logout.html");
    selenium.type("username", "myuser");
    selenium.type("password", "password");
    selenium.click("//input[@value='LOGIN']");
    selenium.waitForPageToLoad("30000");
    selenium.click("Confirm");
    selenium.waitForPageToLoad("30000");
    selenium.click("link=Applications");
    selenium.waitForPageToLoad("30000");
    selenium.click("link=Journey");
    selenium.waitForPageToLoad("30000");
    selenium.click("link=Launch Application (MUST BE LOGGED IN)");
    selenium.waitForPageToLoad("30000");
    if((selenium.isTextPresent("Please enter one of the following:")))
    {
        System.out.println("Journey Working Fine");
        flag_test= 0;
    }
    else
    {
        System.out.println("Journey Failed");
        flag_test = 1;
    }
    selenium.selectFrame("topmenu");
    selenium.click("link=Home");
}
public static Test suite() {
//method added
return new TestSuite(MyTest.class);
}
public void tearDown(){
//Added . Will be called when the test will complete
selenium.stop();
}

}

then a sendmail gettin the values from the selenium test

      import java.util.*;


         public class SendMail
         {
         public void send()
         {


        MyTest Test = new MyTest();
        if (Test.flag_test==1)
          {
            System.out.println("Journey Failed");
        }
        else if(Test.flag_test==0)
          {
            System.out.println("Journey Working Fine");
          }

} }

main class calling both

        import java.io.*;
     import javax.servlet.*;

 public class Test 
   {
public static void main(String args[]) 
{


    MyTest tes = new MyTest();
            junit.textui.TestRunner.run(tes.suite());

    SendMail se = new SendMail();
    se.send();

}
   }

how do i pass the flag value from MyTest to SendMail

+1  A: 
  • The flag should be public static (I don't see it defined in the code you provided) - i.e.

    public class MyTest {
         public static int flag;
         // the rest of the code
    }
    
  • in send() you can refer to it with MyTest.flag_test

Note, that this is not a good way of passing data, but in your case there isn't anything better.

I think you are doing something that shouldn't be done at all. Here's what I propose:

  • move the code that is changing the flag outside the test
  • include it in the test, in the appropriate place (as if it is there)
  • include it in SendMail as well.

Thus you won't need to invoke the test in order to obtain a flag.

Bozho
There is no static `flag` field on `MyTest` as far as I can see.
danben
why the downvote? I indicated that it is wrong, but this is the only way here. He is not in control of the instantiation of his class, so he can't pass any reference neigther from nor to the Test class
Bozho
@danben yes, I said the flag _should_ be static. I don't see it defined either, but that's the way to go
Bozho
No, you edited your answer after I wrote that comment.
danben
I removed the downvote, but seriously, you can at least admit you corrected it.
danben
well, I first submitted it, than saw it should be reworded, and immediately pressed edit. That was because I first assumed the flag is defined and then didn't see it.
Bozho
A: 

Three ways of achieving this 1. Pass the test as parameter to SendMail (already mentioned) 2. Write a listener on test, (Observable pattern/ PropertyChangeSupport in java) and hook it up. (Best IMO) 3. Write to a Static object which acts as white board and read from there. ( a poor man's message queue)

questzen
but the value is not being passed as mentioned thats the issue
weblearner