Selenium sendKeys not staying in expected element - java

I'm trying to add an xml payload as a string to a text field on a website, using Selenium's sendKeys. My issue is that my code is progressing to the next line before all of my xml string has been entered into the text field. This is what my current test looks like;
#FindBy(id = "macro-param-graph")
private WebElement graphIdXml;
public void invokeGraphXmlPayload(String payload) throws IOException, InterruptedException {
String myGraph = generateStringFromResource("src/test/resources/testDataResources/" + payload);
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOf(graphIdXml));
graphIdXml.sendKeys(myGraph);
btn_Preview.click();
}
I've tried something as simple as trying a Thread.sleep just to see if that helps, but seeing as that happens after Selenium has already done the sendKeys function it hasn't been much help.
I'm not receiving an error message per se, just that the rest of the tests continue to run while the xml string is still being sent, so it just continues while making a mess.
Any help or advice on this would be appreciated.

Removing the xml formatting of the xml file I was converting to string has resolved this issue.
I think it's a case where it was inputting returns in the string when the file had the default xml format. The code in the question above now works without any edits.

Related

HtmlUnit HtmlFileInput.setData() not working

I am trying to upload a file to a website using the HtmlUnit HtmlFileInput class. I have the data in a byte[] array and would like to send it up without writing it to a file first.
I'm trying:
HtmlFileInput fileInput = form.getInputByName("file");
fileInput.setData(data);
HtmlElement button = form.getInputByName("validate");
HtmlPage responsePage = button.click();
This is not working. But, when I try
HtmlFileInput fileInput = form.getInputByName("file");
fileInput.setValueAttribute("file.txt");
HtmlElement button = form.getInputByName("validate");
HtmlPage responsePage = button.click();
Everything works fine. The docs seem to indicate that setData() does exactly what I want to do, but it doesn't seem like any of the HtmlUnit code even uses the data_ variable that is set when setData() is called. The code uses the files_ field which is set when setValueAttribute() is called.
I noticed several old bugs that were opened that talked about similar problems and it says that they were all fixed.
Am I trying to use setData() in a way that it shouldn't be used?
Thanks.
In short - data_ is used by getSubmitNameValuePairs() and there are also unit tests for that (e.g. com.gargoylesoftware.htmlunit.html.HtmlFileInput2Test.setValueAttributeAndSetDataDummyFile()).
The trick here is the missing rest of the file - you have to simulate a bit more if you like to get your stuff uploaded. Please set the Value (to submit a dummy file name) and the content type also to help the server to understand your data.
HtmlFileInput fileInput = form.getInputByName("file");
fileInput.setValueAttribute("dummy.txt");
fileInput.setContentType("text/csv");
fileInput.setData("My file data".getBytes());
I think i have to improve the documentation for this a bit.
If you like we can discuss this or if you like to see a quick fix - simply open an issue on github.

How to assert elements contains text in Selenium using JUnit

I have a page that I know contains a certain text at a certain xpath. In firefox I use the following code to assert that the text is present:
assertEquals("specific text", driver.findElement(By.xpath("xpath)).getText());
I'm asserting step 2 in a form and confirming that a certain attachment has been added to the form.
However, when I use the same code in Chrome the displayed output is different but does contain the specific text. I get the following error:
org.junit.ComparisonFailure: expected:<[]specific text> but was:<[C:\fakepath\]specific text>
Instead of asserting something is true (exactly what I'm looking for) I'd like to write something like:
assert**Contains**("specific text", driver.findElement(By.xpath("xpath)).getText());
The code above does not work obviously but I can't find how to get this done.
Using Eclipse, Selenium WebDriver and Java
Use:
String actualString = driver.findElement(By.xpath("xpath")).getText();
assertTrue(actualString.contains("specific text"));
You can also use the following approach, using assertEquals:
String s = "PREFIXspecific text";
assertEquals("specific text", s.substring(s.length()-"specific text".length()));
to ignore the unwanted prefix from the string.
Two Methods assertEquals and assertTrue could be used.
Here is the usage
String actualString = driver.findElement(By.xpath("xpath")).getText();
String expectedString = "ExpectedString";
assertTrue(actualString.contains(expectedString));
You can also use this code:
String actualString = driver.findElement(By.xpath("xpath")).getText();
Assert.assertTrue(actualString.contains("specific text"));

Upload files using selenium

How to upload files from local via window prompt using selenium webdriver?
I want to perform the following actions:
click on 'Browse' option on the window
from the window prompt go to the particular location in the local where the file is kept
select the file and click on 'Open' to upload the file.
Have you tried using input() on proper file input control?
WebElement fileInput = driver.findElement(By.id("some id"));
fileInput.sendKeys("C:/path/to/file.extension");
I have used below three different ways to upload a file in selenium webdriver.
First simple case of just finding the element and typing the absolute path of the document into it. But we need to make sure the HTML field is of input type. Ex:<input type="file" name="uploadsubmit">
Here is the simple code:
WebElement element = driver.findElement(By.name("uploadsubmit"));
element.sendKeys("D:/file.txt");
driver.findElement(By.name("uploadSubmit"));
String validateText = driver.findElement(By.id("message")).getText();
Assert.assertEquals("File uploaded successfully", validateText);
Second case is uploading using Robot class which is used to (generate native system input events) take the control of mouse and keyboard.
The the other option is to use 'AutoIt' (open source tool).
You can find the above three examples : - File Uploads with Selenium Webdriver
Selenium Webdriver doesn't really support this. Interacting with non-browser windows (such as native file upload dialogs and basic auth dialogs) has been a topic of much discussion on the WebDriver discussion board, but there has been little to no progress on the subject.
I have, in the past, been able to work around this by capturing the underlying request with a tool such as Fiddler2, and then just sending the request with the specified file attached as a byte blob.
If you need cookies from an authenticated session, WebDriver.magage().getCookies() should help you in that aspect.
edit: I have code for this somewhere that worked, I'll see if I can get ahold of something that you can use.
public RosterPage UploadRosterFile(String filePath){
Face().Log("Importing Roster...");
LoginRequest login = new LoginRequest();
login.username = Prefs.EmailLogin;
login.password = Prefs.PasswordLogin;
login.rememberMe = false;
login.forward = "";
login.schoolId = "";
//Set up request data
String url = "http://www.foo.bar.com" + "/ManageRoster/UploadRoster";
String javaScript = "return $('#seasons li.selected') .attr('data-season-id');";
String seasonId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);
javaScript = "return Foo.Bar.data.selectedTeamId;";
String teamId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);
//Send Request and parse the response into the new Driver URL
MultipartForm form = new MultipartForm(url);
form.SetField("teamId", teamId);
form.SetField("seasonId", seasonId);
form.SendFile(filePath,LoginRequest.sendLoginRequest(login));
String response = form.ResponseText.ToString();
String newURL = StaticBaseTestObjs.RemoveStringSubString("http://www.foo.bar.com" + response.Split('"')[1].Split('"')[0],"amp;");
Face().Log("Navigating to URL: "+ newURL);
Driver().GoTo(new Uri(newURL));
return this;
}
Where MultiPartForm is:
MultiPartForm
And LoginRequest/Response:
LoginRequest
LoginResponse
The code above is in C#, but there are equivalent base classes in Java that will do what you need them to do to mimic this functionality.
The most important part of all of that code is the MultiPartForm.SendFile method, which is where the magic happens.

java print the redirected url

original url : http://pricecheckindia.com/go/store/ebay/52440?ref=velusliv
redirected url : http://www.ebay.in/itm/Asus-Zenfone-6-A600CG-A601CG-White-16-GB-/111471688863?pt=IN_Mobile_Phones&aff_source=DA
I need a program that will take the original url and print the redirected url.
How to get this done in java.
public static void main(String[] args) throws IOException, InterruptedException
{
String url = "http://pricecheckindia.com/go/store/ebay/52440?ref=velusliv";
Response response = Jsoup.connect(url).followRedirects(false).execute();
System.out.println(response.url());
}
It seems that you are being redirected via JavaScript code, which Jsoup doesn't support (it is simple HTML parser, not browser emulator). Your choice then is to either use tool which will support JavaScript like Selenium web driver, or parse your page to get url from click here link from
If it is taking too long to redirect, then please click here
text.
You can use Jsoup to get this link by adding to your current code
Document doc = response.parse();
String redirectUrl = doc.select("a:contains(click here)").attr("href");
System.out.println(redirectUrl);
which will return and print
http://rover.ebay.com/rover/1/4686-127726-2357-15/2?&site=Partnership_PRCCHK&aff_source=DA&mpre=http%3A%2F%2Fwww.ebay.in%2Fitm%2FAsus-Zenfone-6-A600CG-A601CG-White-16-GB-%2F111471688863%3Fpt%3DIN_Mobile_Phones%26aff_source%3DDA
so now all we need to do is parse query from this URL to get value of mpre key, which encoded version looks like
http%3A%2F%2Fwww.ebay.in%2Fitm%2FAsus-Zenfone-6-A600CG-A601CG-White-16-GB-%2F111471688863%3Fpt%3DIN_Mobile_Phones%26aff_source%3DDA
but after decoding it will actually represents
http://www.ebay.in/itm/Asus-Zenfone-6-A600CG-A601CG-White-16-GB-/111471688863?pt=IN_Mobile_Phones&aff_source=DA
To get value of this key and decode it you can use one of solutions from this question: Parse a URI String into Name-Value Collection. With help of method from accepted answer in previously mentioned question we can just invoke
URL address = new URL(redirectUrl);
Map<String,List<String>> urlQuerryMap= splitQuery(address);
String redirected = urlQuerryMap.get("mpre").get(0);
System.out.println(redirected);
to see result
http://www.ebay.in/itm/Asus-Zenfone-6-A600CG-A601CG-White-16-GB-/111471688863?pt=IN_Mobile_Phones&aff_source=DA

Using Jsoup to extract text from specific class

i'm trying to work with a little android at the moment.
Before all the hate - Yes I have tried searching and found answers related to mine but I simply couldnt get mine to work with the way they did it :/.
I have found out that Jsoup is rather good for parsing data from HTML to use in an app.
So I have trying to recieve data from here Krak
So when I enter the input for a number lets say "86202710"
The link will be Number link
I have then tried to extract the name of the owner of the given number which is "Jens Fisker Automobiler A/S". But I cant seem to get this text out into my textview.
Hope you guys can guide me alittle...
I get an exception "NetworkOnMainThreadException" - AndroidBlockGuardPolicy.onNetwork"
Here is the code I have written to the method for extracting the owner of the number
public void getData() throws IOException{
URL url = new URL("http://mobil.krak.dk/h/#companyResult&searchWord=86202710");
Document doc = Jsoup.parse(url, 3000);
Element content = doc.select("p[header bold]").first();
text = (TextView) findViewById(R.id.tv);
text.setText(content.text());
}
You have to run your networkcode in a AsyncTask on Android. Everything else will fail.
See here: Error when parsing Html using Jsoup
Do you connect through a proxy?
Please check if you have the settings like posted here: Android NetworkOnMainThreadException inside of AsyncTask
Btw. better use connect() method than parse() for such connections:
public void getData() throws IOException{
// You can use a simple string as url
final String url = "http://mobil.krak.dk/h/#companyResult&searchWord=86202710";
// Connect to url and parse it's content
Document doc = Jsoup.connect(url).get(); // Timeout is set to 3 Sec. per default
// Everything else stays same
Element content = doc.select("p[header bold]").first();
text = (TextView) findViewById(R.id.tv);
text.setText(content.text());
}

Categories