I am using Appium for automating my Android app. I have a fragment to enter email and after verifying that second fragment with passwordfield will be loaded. I am able to find all elements in the first fragment; but elements in the second fragment can't be found using any of the methods like By, PageFactory + #AndroidFindBy. Can someone provide a help to sort this issue?
Use ExplicitWait method.
public static void ExplicitWait(MobileElement element){
(new WebDriverWait(driver,30)).until(ExpectedConditions
.elementToBeClickable(element));
}
then before you use element of second fragment call ExplictWait
ExplictWait(passwordField);
passwordField.sendKeys("your password");
Related
I have a fragment screen where there is a form that is used to create Questions and Answers for my app. For this fragment, I use data binding, then I created many functions to validate the form, and check other stuffs.
Now, I'm creating a different fragment screen, where I'll be able to edit this Questions and Answers that were created, and for this, I want to use the same functions that were used when I created on the other fragment, for example to validate the fields that the user is editing.
I thought about implementing an Interface, and put these common functions there, so I could use it on both fragments. However, in these functions I use DataBinding, and I don't know how I can use it on the interface, so it would get the correct XML variables regarding to one fragment, or the other one.
On the screenshot bellow, it shows that I'm trying to use the binding, however I can't specify which one I'm using, otherwise the code will work only for a fragment, and not for both. Consequently, I tried to declare as DataBindingUtil but it didn't work.
Screenshot interface
If you want to go with this approach you could add the views as fields in the interface:
interface IQuestionForm {
var newQuestionTextInput: EditText
var answer1TextField: EditText
fun validateAllParametersToCreateNewQuestion(){
var allTextInputSet = true
if (newQuestionTextInput.text.isNullOrEmpty()){
newQuestionTextInput.error = "You have to enter the question"
allTextInputSet = false
}
if (answer1TextField.text.isNullOrEmpty()){
answer1TextField.error = "You have to enter an answer"
allTextInputSet = false
}
....
}
}
Then initialise those fields after creating the binding in the fragment.
I need to go to the same page on a PDF document after screen change. I have found how to navigate to page here but I failed to get the current page number.
I have found some codes regarding this in MuPDF library but couldn't call it from main activity. In PageView class (MuPDFPageView extends PageView):
public int getPage() {
return mPageNumber;
}
The problem is I don't know how to call this from main activity. I tried something like this:
final MuPDFPageView pageView = new MuPDFPageView(getApplicationContext(), filePickerSupport, core, parentSize, sharedHqBm);
int i = pageView.getPage();
But failed to fill the parameters in. Anybody has a better or easier way to get the current page number?
Within MuPDFActivity, you can call:
mDocView.getDisplayedViewIndex()
and that will return you the current page number.
I'm trying to do something very simple. I have 2 tabs, one showing information about the breed of a dog and the other is showing a list of reviews that people have written describing the breed. I would like to include the number of reviews that were written in the Tab as well so it would look like so:
Breed Info | Reviews(19)
The issue is that I need to download said review data first in order to see the number of reviews there is on the tab.
So what I thought of as a solution was to add the tab first in my BreedProfile.java (ShelockActivity)
ActionBar.Tab reviewTab= ab.newTab();
reviewTab.setText("Reviews");
mTabsAdapter.addTab(reviewTab, DogReviewFragment.class, null);
Then in my BreedProfileReviewFragment.java I would download the reviews then update the reviewTab text by doing so:
getActivity().getActionBar().getTabAt(1).setText("Reviews 10");
However, the above method does not exist according to the LogCat.
03-05 18:15:26.460: E/AndroidRuntime(1286): FATAL EXCEPTION: main
03-05 18:15:26.460: E/AndroidRuntime(1286): java.lang.NoSuchMethodError: android.support.v4.app.FragmentActivity.getActionBar
So my question is, is it possible to access the TAB from the FRAGMENT to change the TEXT property of the TAB after it is added? If it is, how can I achieve that?
NOTES:
I cannot download the reviews together with the breed information because the API's are separate.
The BreedProfile.java Originates from a list of Breeds in a ListActivity BreedList.java.
You can implement an Interface. so you can run the code at the parent activity of the fragment.
create your interface in your fragment like below:.
public interface OnQuestionSelectedListener {
void onQuestionSelected(int Position, boolean isFirstTime);
}
Implement it at your parent activity side like below:
public class YourParentActivity extends Activity implements YourFragment.OnQuestionSelectedListener{
#Override
public void onQuestionSelected(int position, boolean isFirstTime) {
// your code.
}
}
Call it from your fragment like below:
((YourParentActivityName)getActivity()).onQuestionSelected(position, true);
(You can make changes in the code as per your need)
It is a very good practice to transfer all your logic of fragment to its parent because what I believe is fragment use for your UI part and Interface provide us a good way to transfer the fragment message to the parent.Do all the activity at parent and transfer the result to corresponding fragment.
Hope this will help.
In my app I have a header with icon hidden, I have a adapter with a listview when I click the listview I go to a login screen using listener, when the login is successful is should come back to listview(adapter) and icon should get visible on header.
In the login activity I have the following code:
public void onClick(View v) {
String password = etPassword.getText().toString();
if(password.equals("guest")){
SearchAdapter.setImgVisibility();
} else {
//-----
}
finish();
}
In my adapter I am calling the setImgVisibility() as follows, but it is not working
public static void setImgVisibility() {
img.setVisibility(View.VISIBLE);
}
I am getting a Nullpointerexception near the line img.setVisibility(View.VISIBLE);
I am stuck here and don't know what I am doing wrong. Any suggestions or help is appreciated
I would imagine that img is null. You need to look at where this value is set and make sure happens before you call the method setImgVisibility.
Show more of your complete code for people to help further.
Additionally, i've just noticed you've used a static reference to your search adapter, you should be really careful using statics, especially where any referencing of images is concerned as images can be bound to the context, as such unless you nullify the static you will end up with a memory leak. (this used to be an old problem, not sure its still valid, but i would still avoid using a static reference).
Without more code we're not likely to be able to properly help you. For example are you switching activities when logging in? If you are, this won't work at all.
[given the comment below] If you switch activities then your activity containing the list view is going to be destroyed and then rebuilt then you navigate back to it. or it will at least go through the activity lifecycle. This means you can set the icon during the instantiation of the header img.
You could store your logged in state as a property of the Application or a preference. Grab this value when you set the header image and set the image accordingly.
your img object is null. Is your img object is same as View v then you can pass v in setImgVisibility() and then set v.setVisibility(View.VISIBLE)
I'm not familiar with FragmentPagerAdapter, so this is going to be one of those questions that we (you) read the description critically.
Structure: I have a FragmentPagerAdapter (code below), that will hold two fragments at a time. The first displays book excerpts, and the second a list of book titles.
Goal: I want to achieve what is described in the title: the user can navigate to the second fragment in the pager, click on a title, and then I want to move the user back to the first fragment and tell the first fragment to update the text. The first fragment has a triggerRefresh method for that.
Code: I believe my problem happens because of the way FragmentPagerAdapter reuses/creates the Fragments (which I don't understand). This is my class:
static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new ExcerptsFragment();
case 1:
return new BookListFragment();
default:
throw new IllegalArgumentException("not this many fragments: " + position);
}
}
}
This is how I created the relevant members:
ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
MyFragmentPagerAdapter mFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mFragmentPagerAdapter);
And this is what I've tried elsewhere in my Activity, when I receive the callback from the book titles Fragment with the title selected:
mViewPager.setCurrentItem(0); // back to excerpts screen page. It's OK.
// Here's the problem! How to identify the fragment 0
// to ExcerptsFragment and call its triggerRefresh()?!?
Series of problems:
Calling the adapter's getView() won't work because it will return a new instance of ExcerptsFragment, which is not the one currently attached (as expected, throws the exception).
I've seen many people here (example) just storing fragments in the getView(). Is that right? Because by looking at the official examples, seems like an anti-pattern to me (defeat the automatic reference by holding the items). And that is also the opinion here and here (and looks right to me).
Any suggestions? I wouldn't be surprised if I'm not understanding all of this one bit...
Disclaimer: Although this had worked perfectly fine for me before, you should be aware of the classic pitfalls of depending on internal, private behavior. While I wrote tests that would eventually warn me if the internal implementation changed, I have since moved on to greener pastures. And you should, too. As such, the value of this question and its answer is only historical, in my opinion.
Sorry about that question, I think it was the hour.
To solve that problem, I implemented this solution as is. Seems to work just fine. So, I believe it was just a matter of finding the (currently attached) fragment instance by figuring out how its Id is named. The link above explains how it's made.
I opted to answer my own question instead of deleting it because I believe novices like me on these pagers will benefit from a "real case scenario". Most of the answers I've seen talk most about the theory, which is the right way BTW... but without a real example to work on sometimes people like me get lost.
Anyway, here is the last piece of code that I needed (the commented part above):
int n = 0;
mViewPager.setCurrentItem(n); // in the question I had stopped here.
ExcerptsFragment f = (ExcerptsFragment) ContainerActivity.this
.getSupportFragmentManager().findFragmentByTag(getFragmentTag(n));
f.triggerRefresh();
// ... below the helper method: used the solution from the link.
private String getFragmentTag(int pos){
return "android:switcher:"+R.id.pager+":"+pos;
}
So, I'm having a feeling that this is a robust solution, because I'm not holding any references to fragments (thus risking the references being outdated). I kept my own code at a minimum, therefore minimizing the chances of me doing something stupid.
Of course, if you have something to add, to show us, to tell what is wrong in doing it or what can be improved, I'll be glad to hear from you.
I searched for a solution to this problem a while myself. Your approach in principle works, but it will break your code if ever the code of the fragment tag creation in the Android base class implementation changes. This is a quite nasty dependency!
A more elegant approach would be to turn the problem around and keep an instance of your base activity in your fragment. Implement a setter for the tag in your activity and call that inside the fragment upon creation - the tag there is simply available with getTag().
An example implementation can be found here.
I solved this problem by using WeakReferences to the fragments upon creation. See : https://stackoverflow.com/a/23843743/734151
If you find anything wrong with this approach, please comment.