I have a requirement where I want to test the below scenario with Selenium:
Open the xhtml page and the value in the datatable is 10.
Change the value in the database from 10 to 20
Click on the refresh button(Without opening the same xhtml page again) and value should be 20 now.
Below is what I am doing. But no success..
#Test
#InSequence(1)
public void addTestData() {
Warp.initiate(new Activity() {
#Override
public void perform() {
OurPage page = OurPage.on(selenium).withPath(SetupTestData.PATH);
page.addTestDataFor(REFRESH_SHOULD_WORK);
}
}).inspect(new Inspection() {
private static final long serialVersionUID = 1L;
});
}
#InSequence(2)
#Test
public void refreshShouldWork() {
Warp.initiate(new Activity() {
#Override
public void perform() {
CountryDetailsPage page = new CountryDetailsPage(selenium).withPath(CountryDetailsPage.PATH);
page.open();
System.out.println("*** PAGE OPENED ****");
waitGui().until().element(page.table(PROGRESSES_TABLE)).is().present();
assertThat(page.table.row(FIRST).cell(FIFTH).text(), is("10"));
// FIXME: Need to find out the way to change the value
page.refresh().click();
System.out.println("*** CLICKED REFRESH ****");
waitGui().until().element(page.table(PROGRESSES_TABLE)).is().present();
assertThat(page.table.row(FIRST).cell(FIFTH).text(), is("20"));
}
}).inspect(new Inspection() {
private static final long serialVersionUID = 1L;
#AfterPhase(RENDER_RESPONSE)
public void beforeRenderResponse() {
System.out.println("*** AFTER RENDER RESPONSE****");
}
#AfterPhase(Phase.APPLY_REQUEST_VALUES)
public void afterRenderResponse() {
System.out.println("*** AFTER APPLY REQUEST VALUES ****");
}
});
}
If you have any idea how can I achieve this, please let me know. Thanks
Being that Selenium automates only the UI, you should let a different technology handle the database. Selenium by itself is unable to connect to any database and change what you want.
You can use Java to do this. More specifically, use Java Database Connectivity (JDBC).
Here's an example that Vogella uses
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
connect = DriverManager
.getConnection("jdbc:mysql://localhost/feedback?"
+ "user=sqluser&password=sqluserpw");
// statements allow to issue SQL queries to the database
statement = connect.createStatement();
// resultSet gets the result of the SQL query
resultSet = statement
.executeQuery("select * from FEEDBACK.COMMENTS");
writeResultSet(resultSet);
// preparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into FEEDBACK.COMMENTS values (default, ?, ?, ?, ? , ?, ?)");
// "myuser, webpage, datum, summary, COMMENTS from FEEDBACK.COMMENTS");
// parameters start with 1
preparedStatement.setString(1, "Test");
preparedStatement.setString(2, "TestEmail");
preparedStatement.setString(3, "TestWebpage");
preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11));
preparedStatement.setString(5, "TestSummary");
preparedStatement.setString(6, "TestComment");
preparedStatement.executeUpdate();
...
} catch (Exception e) {
throw e;
} finally {
close();
}
}
Update the above code to update the actual mysql field, and you'll be golden. Though I don't think this is a good test, as again, the UI should be making the change and Selenium should be detecting it. This might be a good candidate for an integration test. Not a Selenium test.
Related
I have a Modal class, when this modal is opened, it shows a panel asking the user if the user wants to proceed with the operation. If the user selects Yes the request is sent to the DB, which takes some time, during this time the first panel should be replaced by the second (which displays a spinner). This indeed happens if we do not use the third panel. Although I want to replace the second panel by the third panel in order to inform the user if the operation was successful or not (which depends of the message object,if it is null or have an error message).
So when I use addNewPanel(panel3, target) I never see panel2. I put a thread.sleep(5000) instruction after addNewPanel(panel2, target) and even so, this panel didn't appeared, I only get the initial and panel3 in the end.
If I do not use panel3 I see panel2.
Does anyone have an idea why is this happening?
Below I have the code of the Modal class
public class DetailsModal2 extends Modal<IModel<UserDomain>>{
#SpringBean
private IService service;
private BootstrapAjaxLink<String> noButton;
private ResponseMessage message;
private ProcessingPanel panel2;
private AlertPanel panel3;
private Panel replacedPanel;
public DetailsModal2(String id, IModel<UserDomain> model){
super(id);
replacedPanel = new AreYouSure("replacedPanel");
replacedPanel.setOutputMarkupId(true);
add(replacedPanel);
panel2 = new ProcessingPanel("replacedPanel");
panel3 = new AlertPanel("replacedPanel");
addButton(new BootstrapAjaxLink<String>("button", null, Buttons.Type.Warning, new ResourceModel("details")){
private static final long serialVersionUID = 1L;
#Override
public void onClick(AjaxRequestTarget target) {
// TODO Auto-generated method stub
//I was expecting to see this panel
addNewPanel(panel2,target);
// this puts this button invisible
this.setVisible(false);
target.add(this);
//this changes the label of the No button to Close
noButton.setLabel(Model.of("Close"));
target.add(noButton);
if(!service.retrieveData())
{
message = service.addUser("X");
if(message == null){
panel3.updateClassAndText(true);
addNewPanel(panel3,target);
}
else {
panel3.updateClassAndText(false);
addNewPanel(panel3,target);
System.out.println(""+ message.getError());
}
}//close if
else if(service.retrieveData())
{
message = service.removeUser("X");
if(message == null){
panel3.updateClassAndText(true);
addNewPanel(panel3,target);
}
else{
panel3.updateClassAndText(false);
addNewPanel(panel3,target);
System.out.println(""+ message.getError());
}
}
else{
System.out.println("It was not possible to access the db");
}
}
}
});
noButton = new BootstrapAjaxLink<String>("button", null, Buttons.Type.Primary){
private static final long serialVersionUID = 1L;
#Override
public void onClick(AjaxRequestTarget target) {
close(target);
}
}.setLabel(Model.of("No"));
addButton(noButton);
}
public void addNewPanel(Panel addpanel, AjaxRequestTarget target ){
Panel newPanel = null;
newPanel = addpanel;
newPanel.setOutputMarkupId(true);
replacedPanel.replaceWith(newPanel);
target.add(newPanel);
}
}//close class
HTML
<wicket:extend>
<div><span wicket:id="replacedPanel"> </span></div>
</wicket:extend>
Wicket atmosphere is deprecated from wicket 8 and will not be supported anymore, so do not use it ..
I have an app with contextual commands. After triggered a contextual command, it will make a HTTP request with a link and post the result on the card, something like, "Completed!". I want this card to be closed by itself after one second so that the user need not to tap to close it. Once the result card is closed, it will go back to contextual command lists with "Ok, glass" at footer and ready for next command.
May i know how to do that?
private class HTTPRequest extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
try {
if (mWhat.equalsIgnoreCase("GET")) {
// get json via YouTube API
URL url = new URL("http://example.com");
mUrlConnection = (HttpURLConnection)
url.openConnection();
InputStream in = new BufferedInputStream(
mUrlConnection.getInputStream());
int ch;
StringBuffer b = new StringBuffer();
while ((ch = in.read()) != -1) {
b.append((char) ch);
}
mResult = new String(b);
}
} catch (Exception e) {}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mTvInfo.setText(mResult);
}
You can use an Android Dialog for this:
Use CardBuilder to create the "Completed" card using the MENU layout.
Create a new instance of Dialog and set its content view to be the view returned by CardBuilder.getView.
Show the dialog.
Use Handler.postDelayed (or some similar mechanism) to automatically dismiss the dialog after the desired amount of time has passed.
I am trying to show a card so I know everything up to that point works. However, when I try to display the card, it just goes straight to the home card. The card I was trying to show was just going to display what was said in the voice recognizer before but that didn't work so I just put plain text and that didn't work either. Application goes - voice trigger --> voice recognizer --> this service:
public class MedMinderService extends Service {
public String MedName;
public String voiceResults;
private static final String TAG = "ShowData";
private static final String LIVE_CARD_ID = "showdata";
public static final String PREFS_NAME = "MyPreferencesFile";
private TimelineManager mTimelineManager;
private LiveCard mLiveCard;
#Override
public void onCreate() {
super.onCreate();
mTimelineManager = TimelineManager.from(this);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {
String voiceResults = intent.getExtras()
.getString(RecognizerIntent.EXTRA_RESULTS);
String MedName = voiceResults; //MedName declared
SharedPreferences MedInfo = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = MedInfo.edit();
editor.putString("MedName", MedName.toString());
editor.commit();
mLiveCard = mTimelineManager.getLiveCard(LIVE_CARD_ID);
Intent i = new Intent(this, ShowDataActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return START_STICKY;
}
}
The intent at the bottom goes to this activity:
public class ShowDataActivity extends Activity {
private LiveCard mLiveCard;
public static final String PREFS_NAME = "MyPreferencesFile";
private GestureDetector mGestureDetector;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences MedInfo = getSharedPreferences(PREFS_NAME, 0);
Card ShowDataCard = new Card(this);
ShowDataCard.setText("IT WORKS!");
//ShowDataCard.setText(MedInfo.getString("MedName", "your medication"));
View ShowDataCardView = ShowDataCard.toView();
setContentView(ShowDataCardView);
}
The "ShowDataCard" that has been commented out is what I was origonally trying to do with the voice recognition but it wouldn't even work with the text "IT WORKS!"
Again: I am just trying to show a card with the text "IT WORKS"
thanks
The easiest way to get a live card to appear with just text is using widgets that are compatible with RemoteViews. You can find a list of them in the GDK documentation here:
https://developers.google.com/glass/develop/gdk/ui/live-cards
under the Creating low-frequency live cards section.
Here is some sample code (based on your code above) that can get that working quickly:
final String LIVE_CARD_ID = "showdata";
mLiveCard = mTimelineManager.getLiveCard(LIVE_CARD_ID);
RemoteViews remoteViews =
new RemoteViews(getPackageName(), R.layout.layout_helloglass);
mLiveCard.setViews(remoteViews);
// Make sure Glass navigates to LiveCard immediately
mLiveCard.setNonSilent(true);
mLiveCard.publish();
The layout file can look like this for layout_helloglass.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Hello, Glass!" />
</FrameLayout>
If you still want to navigate to another Activity from your LiveCard, you need to associate the Activity with a PendingIntent and then associate that PendingIntent with the LiveCard's action. This would happen immediately before the LiveCard.publish() method:
Intent i = new Intent(this, ShowCardActivity.class);
mLiveCard.setAction(PendingIntent.getActivity(this, 0, i, 0));
That should get you up and running! Hopefully this will help.
There was a bug in the GDK Sneak Peek that prevented voice prompts from creating Services. If one inserted a Log.d() call in a Service's onStartCommand() override, they would discover that it were never called.
This bug has been fixed in the GDK Preview. This behavior should not appear again.
This question was rewritten after the GDK Preview launch to remove this outdated answer. Thanks to user Falcon for notifying me.
I want to implement auto save functionality.
I have a silverlight application, in which, we are sending data on server on clicking of a button. Now i don't want to click on that button, i want, my data should be post to server periodically with time interval of 20 or 30 seconds.
Plz provide me your valuable suggestion to how to implement this
I use this code to keep a session alive. It does the same thing that you are trying to do; automatically calls a service after specified regular intervals:
public Page()
{
InitializeComponent();
// Set up timer
System.Windows.Threading.DispatcherTimer dt =
new System.Windows.Threading.DispatcherTimer();
// Set to call every 5 minutes
dt.Interval = new TimeSpan(0, 0, 5, 0, 0);
// Set up event handler
dt.Tick += new EventHandler(dt_Tick);
// Start timer
dt.Start();
}
void dt_Tick(object sender, EventArgs e)
{
// Call web service
Ping();
}
void Ping()
{
WebTest.otsref.SilverlightServiceClient webService = new WebTest.SilverlightServiceClient();
webService.PingAsync();
}
I've created a webservice and when I want to use its methods I instantiate it in the a procedure, call the method, and I finally I dispose it, however I think also it could be okay to instantiate the webservice in the "private void Main_Load(object sender, EventArgs e)" event.
The thing is that if I do it the first way I have to instantiate the webservice every time I need one of its methods but in the other way I have to keep a webservice connected all the time when I use it in a form for example.
I would like to know which of these practices are better or if there's a much better way to do it
Strategy 1
private void btnRead_Click(object sender, EventArgs e)
{
try
{
//Show clock
this.picResult.Image = new Bitmap(pathWait);
Application.DoEvents();
//Connect to webservice
svc = new ForPocketPC.ServiceForPocketPC();
svc.Credentials = new System.Net.NetworkCredential(Settings.UserName, Settings.Password);
svc.AllowAutoRedirect = false;
svc.UserAgent = Settings.UserAgent;
svc.PreAuthenticate = true;
svc.Url = Settings.Url;
svc.Timeout = System.Threading.Timeout.Infinite;
svc.CallMethod();
...
}
catch (Exception ex)
{
ShowError(ex);
}
finally
{
if (svc != null)
svc.Dispose();
}
}
Strategy 2
private myWebservice svc;
private void Main_Load(object sender, EventArgs e)
{
//Connect to webservice
svc = new ForPocketPC.ServiceForPocketPC();
svc.Credentials = new System.Net.NetworkCredential(Settings.UserName, Settings.Password);
svc.AllowAutoRedirect = false;
svc.UserAgent = Settings.UserAgent;
svc.PreAuthenticate = true;
svc.Url = Settings.Url;
svc.Timeout = System.Threading.Timeout.Infinite;
}
private void btnRead_Click(object sender, EventArgs e)
{
try
{
//Show clock
this.picResult.Image = new Bitmap(pathWait);
Application.DoEvents();
svc.CallMethod();
...
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void Main_Closing(object sender, CancelEventArgs e)
{
svc.Dispose();
}
It depends on how often you are going to be calling the web service. If you're going to be calling it almost constantly, it would probably be better to use method #2. However, if it's not going to be getting called quite so often, you are better off using method #1, and only instantiating it when you need it.
Right now I made a solution for a mobile device and it turns to be used on irregular times, it could be used in 10 minutes, 1 hour, 4 hours its very variable, it seems that the better aproach is the first strategy.
Last year we went on a project where we used webservices, the fact is that we instantiated our webservices at the Sub New() procedure and it run it very well, however, sometimes some users claimed at us that they woke up from their chairs and when they returned and tried to continue on the application they received a timeout error message and they had to re-login again.
We thougth that maybe that was Ok because maybe the users went out for a very long time out of their seats, but once in a presentation of the application with the CEOs it happened exactly the same scenario and personally I didn't like that behaviour and that's why the question.
Thanks for the answer.