0

I've never really done any serious coding before so please bear with me if I say something incorrect, and just correct me so I know better.

I've create a very simple Selenium IDE test case where it searches a term through google and then waits for a certain link to come up:

Base Url: https://www.google.com/
open | / 
sendKeys | id=lst-ib | This is Sparta${KEY_ENTER}
waitForElementPresent | xpath=(//a[contains(text(),'300 (film) - Wikipedia')])[2]
clickAndWait | xpath=(//a[contains(text(),'300 (film) - Wikipedia')])[2]

And when I export that as a Python2/unittest/WebDriver it gives me this .py file:

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class ThisIsSpartaTestCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.com/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_this_is_sparta_test_case(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("lst-ib").send_keys("This is Sparta", Keys.ENTER)
        for i in range(60):
            try:
                if self.is_element_present(By.XPATH, "(//a[contains(text(),'300 (film) - Wikipedia')])[2]"): break
            except: pass
            time.sleep(1)
        else: self.fail("time out")
        driver.find_element_by_xpath("(//a[contains(text(),'300 (film) - Wikipedia')])[2]").click()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

Which works, and it's all great and dandy.

My issue is that I would like to output a message onto the Python IDLE Shell that says something like "The link element is present" when the python code is able to find the wiki link on the Google page. I tried to do a "print "Wiki Link is Found on Google Page" after the return True in def is_element_present, and even though it runs, it doesn't output that message onto the Shell.

Do any of y'all know how to make it output a message onto the Shell?

Please let me know, thank you!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Maybe one of these options http://stackoverflow.com/questions/284043/outputting-data-from-unit-test-in-python – brennan Apr 06 '17 at 20:13
  • IDLE runs your code in a python subprocess with a wrapper that, among other things, replaces stdin/out/err with objects that communicate with the original process via a socket. `print('Hello')` normally results in 'Hello' appearing in the tkinter Text widget subclass that IDLE uses for Shell. If Selenium re-replaces stdin/out/err, there will be problems. Try running your program directly in python and leave IDLE out of the process. (In general, running one python-coded GUI IDE in another may not work.) – Terry Jan Reedy Apr 07 '17 at 22:30

0 Answers0