Creating a Selenium module to replace testcomplete
<p>I'm trying to replace TestComplete with Selenium for our automated tests, ideally without having to re-write all of the different functions.</p>

<p>The plan is to replicate the Aliases structure for finding elements within TestComplete with a python module that can be dropped in to do the finding of web elements. </p>

<p>I've been able to get this to work for a single page, but due to how python seems to work with imports I've so far been unable to find a way to import beyond this.</p>

<p>Example code within test complete would be.</p>

<p>Aliases.LoginPage.Username.SetText("username")</p>

<p>To replicate this in selenium I've created a Module called Aliases, with a class named LoginPage, containing a property called username.</p>

<p>so Aliases.py looks like below:</p>

<pre><code>    from selenium import webdriver
    driver = webdriver.Chrome("C:\\BrowserDrivers\\chromedriver.exe")

    class LoginPage:
        _username = driver.find_element_by_id("txtUser")

        @property
        def user(self):
            return type(self)._user
</code></pre>

<p>and is called by:</p>

<pre><code>    import Aliases

    login_page = Aliases.LoginPage()
    login_page.username.send_keys("username")
</code></pre>

<p>this works fine with just the one page, however if I add a second class to this module with the find_element code for an element on another page I get an "element not found exception"</p>

<p>Debugging has show that this is because python is trying to set all the class properties within the Aliases module when it is imported, so of course elements not on the login page won't be found.
This occurs even if I specify the class within the Aliases module to import.</p>

<p>Is there a way for me to tell Python to only set the properties for the class being imported
or 
Another way for me to structure the project to replicate the way elements are being found within the current coded test?</p>

<p>Alternatively, am I approaching this the wrong way and I should just start amending the current code we have to be selenium specific? </p>

<p>I don't know the scale of the changes you are talking about but my guess is that it would be faster (and better) to just rewrite the tests than to try to write code so that you don't have to make changes. In the long run, rewriting will likely require less maintenance and be easier to debug. So take the time to rewrite them properly. I did something like this about a year ago when we switched from TestComplete/Javascript to Selenium/Java. I had to learn Java and rewrite everything in Selenium but in the end I'm really happy with the change. Having a strongly typed language, a better IDE (Eclipse), and so on have made me much more productive and the tests are faster, more resilient, and easier to maintain and debug.</p>

python selenium automated-tests testcomplete