Write the output in notepad using Selenium

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Write the output in notepad using Selenium

Karthik
Hi Tarun,

Following is my script in which I have variables a & b... those are getting printed in the console window but I want those in a notepad..can you please tell me how to add the syntax for it?? so that it will create a new notepad in a particular path and write those data in that notepad.

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import com.google.protobuf.ByteString.Output;
import com.thoughtworks.selenium.*;

import org.apache.commons.io.output.WriterOutputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.internal.seleniumemulation.WaitForPageToLoad;
@SuppressWarnings({ "deprecation", "unused" })
public class notepadwrite2 extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.test.com/");
selenium.start();
}
@Test
public void testCase1() throws Exception {
selenium.open("/");
selenium.click("id=bar_city");
selenium.select("id=bar_city", "label=Chennai");
selenium.click("name=imageField");
selenium.waitForPageToLoad("30000");
selenium.click("//img[@onclick=\"_gaq.push(['_trackPageview', '/bricks/propertySearch.html']);\"]");
selenium.click("link=View Contact Details");
String a = selenium.getText("//span[@id='cPersonName#9528957']/b");
String b = selenium.getText("css=span.phoneMar50 > b");
System.out.println(a);
System.out.println(b);
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

softwaretestingforum
Administrator
Hi Karthik,

You could use something like this -

                  FileWriter fileWriter = new FileWriter("out.txt");
                  BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                  bufferedWriter.write("You Text here");
                  bufferedWriter.close();


Replace "You Text here", with your String
Let me know if you need any more info.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

Karthik
Hi Tarun,

Now, I have managed to print the value a in notepad :-)

But, this is the scenario in which I am working now,

A web page has the following listings with values a & b

Listing 1
Listing 2
Listing 3
Listing 4
Listing 5

My script copy the  value a displayed in 'Listing 1' and prints it in a notepad.
How should I substitue the values, so that it should copy all listings a & b values and write in a notepad??

Here is my script,

selenium.open("/");
selenium.click("id=bar_city");
selenium.select("id=bar_city", "label=Chennai");
selenium.click("name=imageField");
selenium.waitForPageToLoad("30000");
selenium.click("//img[@onclick=\"_gaq.push(['_trackPageview', '/bricks/propertySearch.html']);\"]");
selenium.click("link=View Contact Details");
String a = selenium.getText("//span[@id='cPersonName#9528957']/b");

System.out.println(a);
byte buf[] = a.getBytes();
FileOutputStream fi = new FileOutputStream("D:\\data.txt");

for(int i=0; i<buf.length;i++){
        fi.write(buf[i]);
}
fi.write(buf);
}

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

softwaretestingforum
Administrator
Hi Karthik,

I did not quite get this -

Karthik wrote
A web page has the following listings with values a & b

Listing 1
Listing 2
Listing 3
Listing 4
Listing 5

My script copy the  value a displayed in 'Listing 1' and prints it in a notepad.
How should I substitue the values, so that it should copy all listings a & b values and write in a notepad??
Could you elaborate a little more?

n.b. you are the first proud poster in this forum
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

Karthik
Hi Tarun, Its really a very useful blog and am very happy to be the first poster

And for your reference,
(For eg) there will be 10 ad's in this page,
http://www.freeads.in/property-real-estate/

I have to click the 'View Details' button, and copy the data for eg. Name & Number and print it in a notepad.
In the same way I need to automate the script so that it will click on all 10 ad's, copy the name & number and write it in the same notepad. But am very much struck with the syntax?? how can i do this???
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

softwaretestingforum
Administrator
I think I get it now.
You could create a map to store values and write them to text file -


     public static void main(String[] args) throws IOException {
                Map<String, String> dataMap = new HashMap<String, String>();
               
                // Put the number and name which you get from Selenium
                dataMap.put("1234567890", "karthik");
                dataMap.put("0987654321", "tarun");
                // Some more values here

                FileWriter fileWriter = new FileWriter("out.txt");
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
               
                for(String data:dataMap.keySet()) {
                        bufferedWriter.write("number: " +data+ ", name: "+dataMap.get(data) +"\n");
                }
                bufferedWriter.close();
        }


You should get output like -

number: 1234567890, name: karthik
number: 0987654321, name: tarun

=====

Help me spread this forum. Do click on +1 and FB share on LHS, if this forum helps you.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

Karthik
Hi Tarun,

I have mailed you my test script... can you please tell me how can i include your syntax so that it will go to the particular page and copy / write the values in the notepad. Am sorry for taking your time :-)

Thanks,
Karthik.S
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

softwaretestingforum
Administrator
Can you paste you script in right format herein. Your email had it badly formatted.
See my second last post which has code snippets. It makes it easy to understand.
I ask you to post it here so that others could also benefit from it.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

Karthik
Sorry for the formatting, I saw it now oly :-)

Here is my script.

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.DataInputStream;

import com.google.protobuf.ByteString.Output;
import com.thoughtworks.selenium.*;

import org.apache.commons.io.output.WriterOutputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.internal.seleniumemulation.WaitForPageToLoad;
@SuppressWarnings({ "deprecation", "unused" })
public class notepadwrite extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.magicbricks.com/");
selenium.start();
}
@Test
public void testCase1() throws Exception {
selenium.open("/");
selenium.click("id=bar_city");
selenium.select("id=bar_city", "label=Chennai");
selenium.click("name=imageField");
selenium.waitForPageToLoad("30000");
selenium.click("//img[@onclick=\"_gaq.push(['_trackPageview', '/bricks/propertySearch.html']);\"]");
selenium.click("link=View Contact Details");
String a = selenium.getText("//span[@id='cPersonName#9528957']/b");
//String b = selenium.getText("css=span.phoneMar50 > b");

System.out.println(a);
byte buf[] = a.getBytes();
FileOutputStream fi = new FileOutputStream("D:\\data.txt");

for(int i=0; i<buf.length;i++){
        fi.write(buf[i]);
}
fi.write(buf);
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

softwaretestingforum
Administrator
I suppose you don't have any issue till here -

Karthik wrote
String a = selenium.getText("//span[@id='cPersonName#9528957']/b");
String b = selenium.getText("css=span.phoneMar50 > b");
}
I also assume that you would reading these values again and again using some sort of loop.
Now having read values a and b  you could store it in map -

Map<String, String> dataMap = new HashMap<String, String>();
               
                dataMap.put(a, b);
                // Some more values here

                FileWriter fileWriter = new FileWriter("out.txt");
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
               
                // Iterate through values in map and write them to file.
                for(String data:dataMap.keySet()) {
                        bufferedWriter.write("number: " +data+ ", name: "+dataMap.get(data) +"\n");
                }
                bufferedWriter.close();
        }

hope this helps. I would also suggest you to use page object pattern instead of writing test this way.
This post of mine might help you with page object pattern -
http://www.seleniumtests.com/2012/01/selenium-2-methods-are-no-more-weird.html?utm_source=BP_recent
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

Karthik
Tarun, am getting error in this line,

String b = selenium.getText("css=span.phoneMar50 > b");

ERROR: Element css=span.phoneMar50 > b not found

can you tell me how to handle this??
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

softwaretestingforum
Administrator
Can you post the html of element which you are trying to access?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

Karthik
Here is the HTML,

Mobile: 9176604238, 9176987724
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

softwaretestingforum
Administrator
No that is text and not html.
Use Firebug in Firefox to find html of element.

You element locator - "css=span.phoneMar50 > b" is not right and I want to see html corresponding to this locator
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

Karthik
In reply to this post by Karthik
Sorry I think its displaying with HTML conversion "Mobile: tyle="">9176604238, 9176987724"
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

softwaretestingforum
Administrator
I need html like this -

"<input id="subject" name="subject" value="Re: Write the output in notepad using Selenium" size="60" tabindex="1" type="text">"

else upload a small file under > More > Upload a File

You can also preview your message using "Preview Message" button before posting
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

Karthik
This is the HTML,

<div class="pad_tp5 pad_btm1"><div style="" id="contactNo#9528957">Mobile: 9176604238, 9176987724Landline: 91-044-45528474</div></div>
Reply | Threaded
Open this post in threaded view
|

Re: Write the output in notepad using Selenium

softwaretestingforum
Administrator
try this -

Karthik wrote
<div class="pad_tp5 pad_btm1"><div style="" id="contactNo#9528957">Mobile: 9176604238, 9176987724Landline: 91-044-45528474</div></div>
selenium.getText("span.phoneMar50");
~ seleniumtests.com