Browse Source

Integration with FAKER

bernhardbuhl 4 years ago
parent
commit
1653346899

+ 17 - 0
baangt/TestSteps/TestStepMaster.py

@@ -7,6 +7,7 @@ from pkg_resources import parse_version
 import logging
 from baangt.TestSteps.Exceptions import baangtTestStepException
 from baangt.TestSteps.AddressCreation import AddressCreate
+from baangt.base.Faker import Faker as baangtFaker
 
 logger = logging.getLogger("pyC")
 
@@ -31,6 +32,7 @@ class TestStepMaster:
         self.globalRelease = self.testRunInstance.globalSettings.get("Release", "")
         self.ifActive = False
         self.ifIsTrue = True
+        self.baangtFaker = None
 
         if not isinstance(lTestStep, str):
             # This TestStepMaster-Instance should actually do something - activitites are described
@@ -316,6 +318,10 @@ class TestStepMaster:
         The syntax for variables is currently $(<column_name_from_data_file>). Multiple variables can be assigned
         in one cell, for instance perfectly fine: "http://$(BASEURL)/$(ENDPOINT)"
 
+        There's a special syntax for the faker module: $(FAKER.<fakermethod>).
+
+        Also a special syntax for API-Handling: $(APIHandling.<DictElementName>).
+
         @param expression: the current cell, either as fixed value, e.g. "Franzi" or with a varible $(DATE)
         @return: the replaced value, e.g. if expression was $(DATE) and the value in column "DATE" of data-file was
             "01.01.2020" then return will be "01.01.2020"
@@ -344,6 +350,9 @@ class TestStepMaster:
 
                 if dictVariable == 'ANSWER_CONTENT':
                     center = self.apiSession.session[1].answerJSON.get(dictValue, "Empty")
+                elif dictVariable == 'FAKER':
+                    # This is to call Faker Module with the Method, that is given after the .
+                    center = self.__getFakerData(dictValue)
                 else:
                     raise BaseException(f"Missing code to replace value for: {center}")
 
@@ -352,3 +361,11 @@ class TestStepMaster:
 
             expression = "".join([left_part, center, right_part])
         return expression
+
+    def __getFakerData(self, fakerMethod):
+        if not self.baangtFaker:
+            self.baangtFaker = baangtFaker()
+
+        logger.debug(f"Calling faker with method: {fakerMethod}")
+
+        return self.baangtFaker.fakerProxy(fakerMethod=fakerMethod)

+ 28 - 0
baangt/base/Faker.py

@@ -0,0 +1,28 @@
+import logging
+from faker import Faker as FakerBase
+from random import randint
+
+logger = logging.getLogger("pyC")
+
+class Faker:
+    def __init__(self, locale="en-US"):
+        self.faker = FakerBase()
+        # Start with a random number
+        self.faker.seed_instance(randint(10,1000))
+
+    def fakerProxy(self, fakerMethod="email", **kwargs):
+        """
+        Dynamically call Faker's method and return the value back to the caller
+
+        :param fakerMethod: Default "email"
+        :param kwargs: Any arguments needed to execute a specific functionality
+        :return: the value, that was delivered by Faker.
+        """
+        lValue = None
+        try:
+            lCallFakerMethod = getattr(self.faker, fakerMethod)
+            lValue = lCallFakerMethod(**kwargs)
+        except Exception as e:
+            logging.error(f"Error during Faker-Call. Method was: {fakerMethod}, kwargs were: {kwargs}, Exception: {e}")
+
+        return lValue

+ 37 - 0
docs/Variables.rst

@@ -0,0 +1,37 @@
+Dealing with variables
+======================
+
+You know the basic variable format ``$(ColumnFromDataFile)`` which works in Locators and value fields.
+
+For instance:
+    * ``//@id[$(SomeColumnName)]`` will replace the locator at run time with the content of the data file of column ``SomeColumnName``
+    * ``$(URL)`` in the Value 1 or Value 2 will replace the Value at run time with the content of the data file of column ``URL``
+
+You may combine several variables into one expression
+    * ``http://($(BASEURL)-$(URLPART)`` will work, if your data file has the columns ``BASEURL`` and ``URLPART``.
+      Most probably you guessed it already - Column names are case sensitive. And columns may not be used twice.
+
+Special variables for APIs
+--------------------------
+TODO: Write Doku.
+
+Faker
+-----
+
+From Version 2020.04.6rc4 (April 2020) you can also use all the methods, that the famous python module ``Faker`` provides.
+
+The syntax is:
+``$(FAKER.<methodName>)``
+
+Examples:
+    * ``$(FAKER.email)`` will generate random E-Mail addresses
+    * ``$(FAKER.name)`` will generate a random name
+
+To see all the methods, head over to https://faker.readthedocs.io/en/stable/fakerclass.html. Because you use ``baangt``
+you can use all Faker Methods without writing a single line of code.
+
+Info for Developers
+^^^^^^^^^^^^^^^^^^^
+
+Source in ``baangt.base.Faker.py``. Called from ``baangt.TestSteps.TestStepMaster.py`` from ``__getFakerData``.
+Currently it is not supported to hand over parameters.

BIN
docs/_build/doctrees/docs/baangt.TestSteps.doctree


BIN
docs/_build/doctrees/docs/baangt.base.doctree


BIN
docs/_build/doctrees/docs/baangt.ui.doctree


BIN
docs/_build/doctrees/environment.pickle


+ 3 - 0
docs/_build/html/docs/baangt.TestSteps.html

@@ -203,6 +203,7 @@
 <code class="sig-name descname">checkLinks</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#baangt.TestSteps.TestStepMaster.TestStepMaster.checkLinks" title="Permalink to this definition">¶</a></dt>
 <dd><p>Will check all links on the current webpage</p>
 <p>Result will be written into “CheckedLinks” in TestDataDict</p>
+<p>If theres a returncode &gt;= 400 in the list, we’ll mark the testcase as failed</p>
 </dd></dl>
 
 <dl class="method">
@@ -234,6 +235,8 @@
 <code class="sig-name descname">replaceVariables</code><span class="sig-paren">(</span><em class="sig-param">expression</em><span class="sig-paren">)</span><a class="headerlink" href="#baangt.TestSteps.TestStepMaster.TestStepMaster.replaceVariables" title="Permalink to this definition">¶</a></dt>
 <dd><p>The syntax for variables is currently $(&lt;column_name_from_data_file&gt;). Multiple variables can be assigned
 in one cell, for instance perfectly fine: “<a class="reference external" href="http://$(BASEURL)/$(ENDPOINT">http://$(BASEURL)/$(ENDPOINT</a>)”</p>
+<p>There’s a special syntax for the faker module: $(FAKER.&lt;fakermethod&gt;).</p>
+<p>Also a special syntax for API-Handling: $(APIHandling.&lt;DictElementName&gt;).</p>
 <p>&#64;param expression: the current cell, either as fixed value, e.g. “Franzi” or with a varible $(DATE)
 &#64;return: the replaced value, e.g. if expression was $(DATE) and the value in column “DATE” of data-file was</p>
 <blockquote>

+ 8 - 0
docs/_build/html/docs/baangt.base.html

@@ -517,6 +517,14 @@ lBasePath = the Path where the script is run</p>
 </dd></dl>
 
 <dl class="method">
+<dt id="baangt.base.Utils.utils.listToString">
+<em class="property">static </em><code class="sig-name descname">listToString</code><span class="sig-paren">(</span><em class="sig-param">completeList</em><span class="sig-paren">)</span><a class="headerlink" href="#baangt.base.Utils.utils.listToString" title="Permalink to this definition">¶</a></dt>
+<dd><p>Returns a concatenated string from a list-object
+:param completeList: any List
+:return: String</p>
+</dd></dl>
+
+<dl class="method">
 <dt id="baangt.base.Utils.utils.openJson">
 <em class="property">static </em><code class="sig-name descname">openJson</code><span class="sig-paren">(</span><em class="sig-param">fileNameAndPath</em><span class="sig-paren">)</span><a class="headerlink" href="#baangt.base.Utils.utils.openJson" title="Permalink to this definition">¶</a></dt>
 <dd></dd></dl>

+ 5 - 0
docs/_build/html/docs/baangt.ui.html

@@ -192,6 +192,11 @@
 <dd></dd></dl>
 
 <dl class="method">
+<dt id="baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder.doTranslateSubmit">
+<em class="property">static </em><code class="sig-name descname">doTranslateSubmit</code><span class="sig-paren">(</span><em class="sig-param">locator</em><span class="sig-paren">)</span><a class="headerlink" href="#baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder.doTranslateSubmit" title="Permalink to this definition">¶</a></dt>
+<dd></dd></dl>
+
+<dl class="method">
 <dt id="baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder.doTranslateType">
 <em class="property">static </em><code class="sig-name descname">doTranslateType</code><span class="sig-paren">(</span><em class="sig-param">locator</em>, <em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder.doTranslateType" title="Permalink to this definition">¶</a></dt>
 <dd></dd></dl>

+ 6 - 2
docs/_build/html/genindex.html

@@ -336,10 +336,10 @@
 </li>
       <li><a href="docs/baangt.TestSteps.html#baangt.TestSteps.TestStepMaster.TestStepMaster.doSaveData">doSaveData() (baangt.TestSteps.TestStepMaster.TestStepMaster method)</a>
 </li>
-  </ul></td>
-  <td style="width: 33%; vertical-align: top;"><ul>
       <li><a href="docs/baangt.ui.html#baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder.doTranslate">doTranslate() (baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder method)</a>
 </li>
+  </ul></td>
+  <td style="width: 33%; vertical-align: top;"><ul>
       <li><a href="docs/baangt.ui.html#baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder.doTranslateClick">doTranslateClick() (baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder static method)</a>
 </li>
       <li><a href="docs/baangt.ui.html#baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder.doTranslategoBackAndWait">doTranslategoBackAndWait() (baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder static method)</a>
@@ -348,6 +348,8 @@
 </li>
       <li><a href="docs/baangt.ui.html#baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder.doTranslateSelect">doTranslateSelect() (baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder static method)</a>
 </li>
+      <li><a href="docs/baangt.ui.html#baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder.doTranslateSubmit">doTranslateSubmit() (baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder static method)</a>
+</li>
       <li><a href="docs/baangt.ui.html#baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder.doTranslateType">doTranslateType() (baangt.ui.ImportKatalonRecorder.ImportKatalonRecorder static method)</a>
 </li>
   </ul></td>
@@ -472,6 +474,8 @@
 <h2 id="L">L</h2>
 <table style="width: 100%" class="indextable genindextable"><tr>
   <td style="width: 33%; vertical-align: top;"><ul>
+      <li><a href="docs/baangt.base.html#baangt.base.Utils.utils.listToString">listToString() (baangt.base.Utils.utils static method)</a>
+</li>
       <li><a href="docs/baangt.katalonImporter.html#baangt.katalonImporter.katalonImport.LocatorObjects">LocatorObjects (class in baangt.katalonImporter.katalonImport)</a>
 </li>
   </ul></td>

BIN
docs/_build/html/objects.inv


File diff suppressed because it is too large
+ 1 - 1
docs/_build/html/searchindex.js


BIN
examples/AutomationPracticeNew.xlsx


+ 3 - 2
examples/globals.json

@@ -1,7 +1,8 @@
 {
     "exportFilesBasePath": "",
     "TC.Lines": "",
-    "TC.dontCloseBrowser": "True",
+    "TC.dontCloseBrowser": "",
     "TC.slowExecution": "",
-    "TC.NetworkInfo": "True"
+    "TC.NetworkInfo": "",
+    "TX.DEBUG": "True"
 }

+ 1 - 0
requirements.txt

@@ -1,6 +1,7 @@
 openpyxl==2.3.5
 pytest
 pydispatch
+faker>=4.0.2
 Appium-Python-Client==0.50
 alabaster==0.7.12
 Babel>=2.8.0