Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

# -*- coding: UTF-8 -*- 

# Copyright 2015-2016 Luc Saffre. 

# License: BSD, see LICENSE for more details. 

"""Used by :xfile:`make_screenshots.py` scripts. 

""" 

 

from __future__ import unicode_literals, absolute_import, print_function 

from builtins import object 

import sys 

import subprocess 

 

from unipath import Path 

from atelier import rstgen 

from atelier.utils import unindent 

 

from selenium import webdriver 

from selenium.webdriver.common.action_chains import ActionChains 

from selenium.webdriver.common.by import By 

from selenium.webdriver.support.ui import WebDriverWait 

from selenium.webdriver.support import expected_conditions as EC 

 

 

def runserver(settings_module, func, driver=None): 

    args = ['django-admin', 'runserver', '--noreload', '--settings', 

            settings_module] 

    server = subprocess.Popen(args, stdout=None, stderr=None) 

 

    # print("Started subprocess {0}".format(server.pid)) 

 

    if driver is None: 

        driver = webdriver.Firefox() 

 

    try: 

        driver.get("http://127.0.0.1:8000/") 

        func(driver) 

    finally: 

        driver.quit() 

        server.terminate() 

 

 

class Album(object): 

    """Generates one directory of screenshots images and their `index.rst` 

    file. 

 

    """ 

    screenshot_root = None 

    screenshots = [] 

    title = None 

    intro = None 

    ref = None 

 

    def __init__(self, driver, root=None, title="Screenshots", 

                 ref=None, intro=None): 

        self.driver = driver 

        self.actionChains = ActionChains(driver) 

 

        if root is not None: 

            self.screenshot_root = Path(root) 

            self.screenshots = [] 

            self.title = title 

            self.intro = intro 

            self.ref = ref 

 

    def checktitle(self, title): 

        if self.driver.title != title: 

            print("Title is {0} (expected: {1})".format( 

                self.driver.title, title)) 

            sys.exit(-1) 

 

    def screenshot(self, name, caption, before='', after=''): 

        filename = self.screenshot_root.child(name) 

        print("Writing screenshot {0} ...".format(filename)) 

        if not self.driver.get_screenshot_as_file(filename): 

            print("Failed to create {0}".format(filename)) 

            sys.exit(-1) 

        before = unindent(before) 

        after = unindent(after) 

        self.screenshots.append((name, caption, before, after)) 

 

    def error(self, msg): 

        raise Exception(msg) 

        # print(msg) 

        # sys.exit(-1) 

 

    def doubleclick(self, elem): 

        self.actionChains.double_click(elem).perform() 

 

    def stabilize(self): 

        """wait until no more loadmask is visible""" 

        WebDriverWait(self.driver, 10).until( 

            EC.invisibility_of_element_located( 

                # (By.CLASS_NAME, "ext-el-mask-msg x-mask-loading"))) 

                (By.CSS_SELECTOR, ".x-mask-loading"))) 

 

    def write_index(self): 

        index = self.screenshot_root.child('index.rst') 

        if self.ref: 

            content = ".. _{0}:\n\n".format(self.ref) 

        else: 

            content = "" 

        content += rstgen.header(1, self.title) 

        content += "\n\n\n" 

        if self.intro: 

            content += unindent(self.intro) 

            content += "\n\n\n" 

 

        for name, caption, before, after in self.screenshots: 

            content += "\n\n" 

            content += rstgen.header(2, caption) 

            content += """ 

 

{before} 

 

.. image:: {name} 

    :alt: {caption} 

    :width: 500 

 

{after} 

 

""".format(**locals()) 

 

        index.write_file(content.encode('utf-8'))